forked from ory/hydra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth2_auth_code_test.go
66 lines (55 loc) · 1.86 KB
/
oauth2_auth_code_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
package oauth2_test
import (
"io/ioutil"
"net/http"
"testing"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/go-errors/errors"
"github.com/julienschmidt/httprouter"
ejwt "github.com/ory-am/fosite/token/jwt"
"github.com/ory-am/hydra/jwk"
. "github.com/ory-am/hydra/oauth2"
"github.com/ory-am/hydra/pkg"
"github.com/pborman/uuid"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
)
func TestAuthCode(t *testing.T) {
var code string
var validConsent bool
router.GET("/consent", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
tok, err := jwt.Parse(r.URL.Query().Get("challenge"), func(tt *jwt.Token) (interface{}, error) {
if _, ok := tt.Method.(*jwt.SigningMethodRSA); !ok {
return nil, errors.Errorf("Unexpected signing method: %v", tt.Header["alg"])
}
pk, err := keyManager.GetKey(ConsentChallengeKey, "public")
pkg.RequireError(t, false, err)
return jwk.MustRSAPublic(jwk.First(pk.Keys)), nil
})
pkg.RequireError(t, false, err)
require.True(t, tok.Valid)
consent, err := signConsentToken(map[string]interface{}{
"jti": uuid.New(),
"exp": time.Now().Add(time.Hour).Unix(),
"iat": time.Now().Unix(),
"aud": "app-client",
})
pkg.RequireError(t, false, err)
http.Redirect(w, r, ejwt.ToString(tok.Claims["redir"])+"&consent="+consent, http.StatusFound)
validConsent = true
})
router.GET("/callback", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
code = r.URL.Query().Get("code")
w.Write([]byte(r.URL.Query().Get("code")))
})
resp, err := http.Get(oauthConfig.AuthCodeURL("some-foo-state"))
pkg.RequireError(t, false, err)
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
pkg.RequireError(t, false, err)
require.True(t, validConsent)
require.NotEmpty(t, code)
_, err = oauthConfig.Exchange(oauth2.NoContext, code)
pkg.RequireError(t, false, err)
}