forked from ory/hydra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsent_strategy.go
120 lines (98 loc) · 2.88 KB
/
consent_strategy.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package oauth2
import (
"fmt"
"time"
"crypto/rsa"
"github.com/dgrijalva/jwt-go"
"github.com/go-errors/errors"
"github.com/ory-am/fosite"
"github.com/ory-am/fosite/handler/oidc/strategy"
ejwt "github.com/ory-am/fosite/token/jwt"
"github.com/ory-am/hydra/jwk"
"github.com/pborman/uuid"
)
const (
ConsentChallengeKey = "consent.challenge"
ConsentEndpointKey = "consent.endpoint"
)
type DefaultConsentStrategy struct {
Issuer string
KeyManager jwk.Manager
}
func (s *DefaultConsentStrategy) ValidateResponse(a fosite.AuthorizeRequester, token string) (claims *Session, err error) {
t, err := jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodRSA); !ok {
return nil, errors.Errorf("Unexpected signing method: %v", t.Header["alg"])
}
pk, err := s.KeyManager.GetKey(ConsentEndpointKey, "public")
if err != nil {
return nil, err
}
rsaKey, ok := jwk.First(pk.Keys).Key.(*rsa.PublicKey)
if !ok {
return nil, errors.New("Could not convert to RSA Private Key")
}
return rsaKey, nil
})
if err != nil {
return nil, errors.Errorf("Couldn't parse token: %v", err)
} else if !t.Valid {
return nil, errors.Errorf("Token is invalid")
}
if time.Now().After(ejwt.ToTime(t.Claims["exp"])) {
return nil, errors.Errorf("Token expired")
}
if ejwt.ToString(t.Claims["aud"]) != a.GetClient().GetID() {
return nil, errors.Errorf("Audience mismatch")
}
subject := ejwt.ToString(t.Claims["sub"])
for _, scope := range toStringSlice(t.Claims["scp"]) {
a.GrantScope(scope)
}
return &Session{
Subject: subject,
DefaultSession: &strategy.DefaultSession{
Claims: &ejwt.IDTokenClaims{
Audience: a.GetClient().GetID(),
Subject: subject,
Issuer: s.Issuer,
IssuedAt: time.Now(),
ExpiresAt: time.Now(),
Extra: t.Claims,
},
Headers: &ejwt.Headers{},
},
}, err
}
func toStringSlice(i interface{}) []string {
r, ok := i.([]string)
if !ok {
return []string{}
}
return r
}
func (s *DefaultConsentStrategy) IssueChallenge(authorizeRequest fosite.AuthorizeRequester, redirectURL string) (string, error) {
token := jwt.New(jwt.SigningMethodRS256)
token.Claims = map[string]interface{}{
"jti": uuid.New(),
"scp": authorizeRequest.GetScopes(),
"aud": authorizeRequest.GetClient().GetID(),
"exp": time.Now().Add(time.Hour).Unix(),
"redir": redirectURL,
}
ks, err := s.KeyManager.GetKey(ConsentChallengeKey, "private")
if err != nil {
return "", errors.New(err)
}
rsaKey, ok := jwk.First(ks.Keys).Key.(*rsa.PrivateKey)
if !ok {
return "", errors.New("Could not convert to RSA Private Key")
}
var signature, encoded string
if encoded, err = token.SigningString(); err != nil {
return "", errors.New(err)
} else if signature, err = token.Method.Sign(encoded, rsaKey); err != nil {
return "", errors.New(err)
}
return fmt.Sprintf("%s.%s", encoded, signature), nil
}