generated from resonatecoop/id-server-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
grant_type_authorization_code.go
63 lines (53 loc) · 1.15 KB
/
grant_type_authorization_code.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package oauth
import (
"context"
"errors"
"net/http"
"github.com/resonatecoop/id/oauth/tokentypes"
"github.com/resonatecoop/user-api/model"
)
var (
// ErrInvalidRedirectURI ...
ErrInvalidRedirectURI = errors.New("Invalid redirect URI")
)
func (s *Service) authorizationCodeGrant(r *http.Request, client *model.Client) (*AccessTokenResponse, error) {
ctx := context.Background()
// Fetch the authorization code
authorizationCode, err := s.getValidAuthorizationCode(
r.Form.Get("code"),
r.Form.Get("redirect_uri"),
client,
)
if err != nil {
return nil, err
}
// Log in the user
accessToken, refreshToken, err := s.Login(
authorizationCode.Client,
authorizationCode.User,
authorizationCode.Scope,
)
if err != nil {
return nil, err
}
// Delete the authorization code
_, err = s.db.NewDelete().
Model(authorizationCode).
WherePK().
ForceDelete().
Exec(ctx)
if err != nil {
return nil, err
}
// Create response
accessTokenResponse, err := NewAccessTokenResponse(
accessToken,
refreshToken,
s.cnf.Oauth.AccessTokenLifetime,
tokentypes.Bearer,
)
if err != nil {
return nil, err
}
return accessTokenResponse, nil
}