-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oidc_token.go
60 lines (48 loc) · 1.85 KB
/
oidc_token.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
package charon
import (
"io"
"net/http"
"gitlab.com/tozd/go/errors"
"gitlab.com/tozd/identifier"
"gitlab.com/tozd/waf"
)
// OIDCTokenPost handler handles requests to issue access and other tokens.
func (s *Service) OIDCTokenPost(w http.ResponseWriter, req *http.Request, _ waf.Params) {
defer req.Body.Close()
defer io.Copy(io.Discard, req.Body) //nolint:errcheck
ctx := req.Context()
oidc := s.oidc()
// Create an empty session object which serves as a prototype of the reconstructed session object.
// For client credentials grant type there is no reconstruction and then we set subject to client's
// ID in those tokens, because client credentials based tokens do not have associated
// user, but represent access for the client itself.
sessionData := new(OIDCSession)
accessRequest, err := oidc.NewAccessRequest(ctx, req, sessionData)
if err != nil {
errE := errors.WithStack(err)
s.WithError(ctx, errE)
oidc.WriteAccessError(ctx, w, accessRequest, errE)
return
}
// Use our identifiers.
id := identifier.New()
accessRequest.SetID(id.String())
if accessRequest.GetGrantTypes().ExactOne("client_credentials") {
// This is used by the client credentials grant type. For implicit
// and explicit flows this is done in the authorization handler.
grantAllAudiences(accessRequest)
grantAllScopes(accessRequest)
session := accessRequest.GetSession().(*OIDCSession) //nolint:errcheck,forcetypeassert
client := accessRequest.GetClient().(*OIDCClient) //nolint:errcheck,forcetypeassert
session.Client = identifier.MustFromString(client.GetID())
session.Subject = client.AppID
}
response, err := oidc.NewAccessResponse(ctx, accessRequest)
if err != nil {
errE := errors.WithStack(err)
s.WithError(ctx, errE)
oidc.WriteAccessError(ctx, w, accessRequest, errE)
return
}
oidc.WriteAccessResponse(ctx, w, accessRequest, response)
}