-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
68 lines (54 loc) · 1.64 KB
/
handler.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
64
65
66
67
68
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package verifiable
import (
"context"
"time"
"authelia.com/provider/oauth2"
"authelia.com/provider/oauth2/internal/consts"
"authelia.com/provider/oauth2/x/errorsx"
)
const (
draftScope = "userinfo_credential_draft_00"
draftNonceField = "c_nonce_draft_00"
draftNonceExpField = "c_nonce_expires_in_draft_00"
)
type Handler struct {
Config interface {
oauth2.VerifiableCredentialsNonceLifespanProvider
}
NonceManager
}
var (
_ oauth2.TokenEndpointHandler = (*Handler)(nil)
)
func (c *Handler) HandleTokenEndpointRequest(ctx context.Context, request oauth2.AccessRequester) error {
if !c.CanHandleTokenEndpointRequest(ctx, request) {
return errorsx.WithStack(oauth2.ErrUnknownRequest)
}
return nil
}
func (c *Handler) PopulateTokenEndpointResponse(
ctx context.Context,
request oauth2.AccessRequester,
response oauth2.AccessResponder,
) error {
if !c.CanHandleTokenEndpointRequest(ctx, request) {
return errorsx.WithStack(oauth2.ErrUnknownRequest)
}
lifespan := c.Config.GetVerifiableCredentialsNonceLifespan(ctx)
expiry := time.Now().UTC().Add(lifespan)
nonce, err := c.NewNonce(ctx, response.GetAccessToken(), expiry)
if err != nil {
return err
}
response.SetExtra(draftNonceField, nonce)
response.SetExtra(draftNonceExpField, int64(lifespan.Seconds()))
return nil
}
func (c *Handler) CanSkipClientAuth(context.Context, oauth2.AccessRequester) bool {
return false
}
func (c *Handler) CanHandleTokenEndpointRequest(_ context.Context, requester oauth2.AccessRequester) bool {
return requester.GetGrantedScopes().Has(consts.ScopeOpenID, draftScope)
}