forked from ory/fosite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
44 lines (36 loc) · 1.34 KB
/
helper.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
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package oauth2
import (
"context"
"time"
"github.com/ory/fosite"
)
type HandleHelperConfigProvider interface {
fosite.AccessTokenLifespanProvider
fosite.RefreshTokenLifespanProvider
}
type HandleHelper struct {
AccessTokenStrategy AccessTokenStrategy
AccessTokenStorage AccessTokenStorage
Config HandleHelperConfigProvider
}
func (h *HandleHelper) IssueAccessToken(ctx context.Context, defaultLifespan time.Duration, requester fosite.AccessRequester, responder fosite.AccessResponder) error {
token, signature, err := h.AccessTokenStrategy.GenerateAccessToken(ctx, requester)
if err != nil {
return err
} else if err := h.AccessTokenStorage.CreateAccessTokenSession(ctx, signature, requester.Sanitize([]string{})); err != nil {
return err
}
responder.SetAccessToken(token)
responder.SetTokenType("bearer")
responder.SetExpiresIn(getExpiresIn(requester, fosite.AccessToken, defaultLifespan, time.Now().UTC()))
responder.SetScopes(requester.GetGrantedScopes())
return nil
}
func getExpiresIn(r fosite.Requester, key fosite.TokenType, defaultLifespan time.Duration, now time.Time) time.Duration {
if r.GetSession().GetExpiresAt(key).IsZero() {
return defaultLifespan
}
return time.Duration(r.GetSession().GetExpiresAt(key).UnixNano() - now.UnixNano())
}