...
1
21
22 package oauth2
23
24 import (
25 "context"
26 "strconv"
27 "strings"
28 "time"
29
30 "github.com/ory/x/errorsx"
31
32 "github.com/ory/fosite"
33 )
34
35
36
37 type AuthorizeImplicitGrantTypeHandler struct {
38 AccessTokenStrategy AccessTokenStrategy
39
40
41 AccessTokenStorage AccessTokenStorage
42
43
44 AccessTokenLifespan time.Duration
45
46 ScopeStrategy fosite.ScopeStrategy
47 AudienceMatchingStrategy fosite.AudienceMatchingStrategy
48 }
49
50 func (c *AuthorizeImplicitGrantTypeHandler) HandleAuthorizeEndpointRequest(ctx context.Context, ar fosite.AuthorizeRequester, resp fosite.AuthorizeResponder) error {
51
52 if !ar.GetResponseTypes().ExactOne("token") {
53 return nil
54 }
55
56 ar.SetDefaultResponseMode(fosite.ResponseModeFragment)
57
58
59
60
61
62
63 if !ar.GetClient().GetGrantTypes().Has("implicit") {
64 return errorsx.WithStack(fosite.ErrInvalidGrant.WithHint("The OAuth 2.0 Client is not allowed to use the authorization grant 'implicit'."))
65 }
66
67 client := ar.GetClient()
68 for _, scope := range ar.GetRequestedScopes() {
69 if !c.ScopeStrategy(client.GetScopes(), scope) {
70 return errorsx.WithStack(fosite.ErrInvalidScope.WithHintf("The OAuth 2.0 Client is not allowed to request scope '%s'.", scope))
71 }
72 }
73
74 if err := c.AudienceMatchingStrategy(client.GetAudience(), ar.GetRequestedAudience()); err != nil {
75 return err
76 }
77
78
79
80
81 return c.IssueImplicitAccessToken(ctx, ar, resp)
82 }
83
84 func (c *AuthorizeImplicitGrantTypeHandler) IssueImplicitAccessToken(ctx context.Context, ar fosite.AuthorizeRequester, resp fosite.AuthorizeResponder) error {
85
86 if ar.GetSession().GetExpiresAt(fosite.AccessToken).IsZero() {
87 ar.GetSession().SetExpiresAt(fosite.AccessToken, time.Now().UTC().Add(c.AccessTokenLifespan).Round(time.Second))
88 }
89
90
91 token, signature, err := c.AccessTokenStrategy.GenerateAccessToken(ctx, ar)
92 if err != nil {
93 return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
94 }
95
96 if err := c.AccessTokenStorage.CreateAccessTokenSession(ctx, signature, ar.Sanitize([]string{})); err != nil {
97 return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
98 }
99 resp.AddParameter("access_token", token)
100 resp.AddParameter("expires_in", strconv.FormatInt(int64(getExpiresIn(ar, fosite.AccessToken, c.AccessTokenLifespan, time.Now().UTC())/time.Second), 10))
101 resp.AddParameter("token_type", "bearer")
102 resp.AddParameter("state", ar.GetState())
103 resp.AddParameter("scope", strings.Join(ar.GetGrantedScopes(), " "))
104
105 ar.SetResponseTypeHandled("token")
106
107 return nil
108 }
109
View as plain text