forked from ory/hydra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth2_test.go
155 lines (130 loc) · 4.08 KB
/
oauth2_test.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package oauth2_test
import (
"net/http/httptest"
"time"
"fmt"
"net/url"
"github.com/dgrijalva/jwt-go"
"github.com/go-errors/errors"
"github.com/julienschmidt/httprouter"
"github.com/ory-am/fosite"
"github.com/ory-am/fosite/handler/core"
"github.com/ory-am/fosite/handler/core/client"
"github.com/ory-am/fosite/handler/core/explicit"
"github.com/ory-am/fosite/handler/core/strategy"
"github.com/ory-am/fosite/hash"
"github.com/ory-am/fosite/token/hmac"
"github.com/ory-am/hydra/jwk"
. "github.com/ory-am/hydra/oauth2"
"github.com/ory-am/hydra/pkg"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
var store = pkg.FositeStore()
var keyManager = &jwk.MemoryManager{}
var keyGenerator = &jwk.RS256Generator{}
var hmacStrategy = &strategy.HMACSHAStrategy{
Enigma: &hmac.HMACStrategy{
GlobalSecret: []byte("some-super-cool-secret-that-nobody-knows"),
},
}
var authCodeHandler = &explicit.AuthorizeExplicitGrantTypeHandler{
AccessTokenStrategy: hmacStrategy,
RefreshTokenStrategy: hmacStrategy,
AuthorizeCodeStrategy: hmacStrategy,
AuthorizeCodeGrantStorage: store,
AuthCodeLifespan: time.Hour,
AccessTokenLifespan: time.Hour,
}
var hasher = &hash.BCrypt{}
var handler = &Handler{
OAuth2: &fosite.Fosite{
Store: store,
MandatoryScope: "hydra",
AuthorizeEndpointHandlers: fosite.AuthorizeEndpointHandlers{
authCodeHandler,
},
TokenEndpointHandlers: fosite.TokenEndpointHandlers{
authCodeHandler,
&client.ClientCredentialsGrantHandler{
HandleHelper: &core.HandleHelper{
AccessTokenStrategy: hmacStrategy,
AccessTokenStorage: store,
AccessTokenLifespan: time.Hour,
},
},
},
AuthorizedRequestValidators: fosite.AuthorizedRequestValidators{},
Hasher: hasher,
},
Consent: &DefaultConsentStrategy{
Issuer: "https://hydra.localhost",
KeyManager: keyManager,
},
}
var router = httprouter.New()
var ts *httptest.Server
var oauthConfig *oauth2.Config
var oauthClientConfig *clientcredentials.Config
func init() {
keys, err := keyGenerator.Generate("")
pkg.Must(err, "")
keyManager.AddKeySet(ConsentChallengeKey, keys)
keys, err = keyGenerator.Generate("")
pkg.Must(err, "")
keyManager.AddKeySet(ConsentEndpointKey, keys)
ts = httptest.NewServer(router)
handler.SetRoutes(router)
store.Clients["app"] = &fosite.DefaultClient{
ID: "app",
Secret: []byte("secret"),
RedirectURIs: []string{ts.URL + "/callback"},
ResponseTypes: []string{"id_token", "code", "token"},
GrantTypes: []string{"implicit", "refresh_token", "authorization_code", "password", "client_credentials"},
}
c, _ := url.Parse(ts.URL + "/consent")
handler.ConsentURL = *c
h, _ := hasher.Hash([]byte("secret"))
store.Clients["app-client"] = &fosite.DefaultClient{
ID: "app-client",
Secret: h,
RedirectURIs: []string{ts.URL + "/callback"},
ResponseTypes: []string{"id_token", "code", "token"},
GrantTypes: []string{"implicit", "refresh_token", "authorization_code", "password", "client_credentials"},
}
oauthConfig = &oauth2.Config{
ClientID: "app-client",
ClientSecret: "secret",
Endpoint: oauth2.Endpoint{
AuthURL: ts.URL + "/oauth2/auth",
TokenURL: ts.URL + "/oauth2/token",
},
RedirectURL: ts.URL + "/callback",
Scopes: []string{"hydra"},
}
oauthClientConfig = &clientcredentials.Config{
ClientID: "app-client",
ClientSecret: "secret",
TokenURL: ts.URL + "/oauth2/token",
Scopes: []string{"hydra"},
}
}
func signConsentToken(claims map[string]interface{}) (string, error) {
token := jwt.New(jwt.SigningMethodRS256)
token.Claims = claims
keys, err := keyManager.GetKey(ConsentEndpointKey, "private")
if err != nil {
return "", errors.New(err)
}
rsaKey, err := jwk.ToRSAPrivate(jwk.First(keys.Keys))
if err != nil {
return "", err
}
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
}