1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
19 "gopkg.in/square/go-jose.v2"
20 "gopkg.in/square/go-jose.v2/jwt"
23 type OIDCProvider struct {
24 // expected token request
27 ValidClientSecret string
28 // desired response from token endpoint
30 AuthEmailVerified bool
34 AccessTokenPayload map[string]interface{}
36 PeopleAPIResponse map[string]interface{}
39 Issuer *httptest.Server
40 PeopleAPI *httptest.Server
44 func NewOIDCProvider(c *check.C) *OIDCProvider {
45 p := &OIDCProvider{c: c}
47 p.key, err = rsa.GenerateKey(rand.Reader, 2048)
48 c.Assert(err, check.IsNil)
49 p.Issuer = httptest.NewServer(http.HandlerFunc(p.serveOIDC))
50 p.PeopleAPI = httptest.NewServer(http.HandlerFunc(p.servePeopleAPI))
51 p.AccessTokenPayload = map[string]interface{}{"sub": "example"}
55 func (p *OIDCProvider) ValidAccessToken() string {
56 buf, _ := json.Marshal(p.AccessTokenPayload)
57 return p.fakeToken(buf)
60 func (p *OIDCProvider) serveOIDC(w http.ResponseWriter, req *http.Request) {
62 p.c.Logf("serveOIDC: got req: %s %s %s", req.Method, req.URL, req.Form)
63 w.Header().Set("Content-Type", "application/json")
65 case "/.well-known/openid-configuration":
66 json.NewEncoder(w).Encode(map[string]interface{}{
67 "issuer": p.Issuer.URL,
68 "authorization_endpoint": p.Issuer.URL + "/auth",
69 "token_endpoint": p.Issuer.URL + "/token",
70 "jwks_uri": p.Issuer.URL + "/jwks",
71 "userinfo_endpoint": p.Issuer.URL + "/userinfo",
74 var clientID, clientSecret string
75 auth, _ := base64.StdEncoding.DecodeString(strings.TrimPrefix(req.Header.Get("Authorization"), "Basic "))
76 authsplit := strings.Split(string(auth), ":")
77 if len(authsplit) == 2 {
78 clientID, _ = url.QueryUnescape(authsplit[0])
79 clientSecret, _ = url.QueryUnescape(authsplit[1])
81 if clientID != p.ValidClientID || clientSecret != p.ValidClientSecret {
82 p.c.Logf("OIDCProvider: expected (%q, %q) got (%q, %q)", p.ValidClientID, p.ValidClientSecret, clientID, clientSecret)
83 w.WriteHeader(http.StatusUnauthorized)
87 if req.Form.Get("code") != p.ValidCode || p.ValidCode == "" {
88 w.WriteHeader(http.StatusUnauthorized)
91 idToken, _ := json.Marshal(map[string]interface{}{
93 "aud": []string{clientID},
94 "sub": "fake-user-id",
95 "exp": time.Now().UTC().Add(time.Minute).Unix(),
96 "iat": time.Now().UTC().Unix(),
97 "nonce": "fake-nonce",
99 "email_verified": p.AuthEmailVerified,
101 "given_name": p.AuthGivenName,
102 "family_name": p.AuthFamilyName,
103 "alt_verified": true, // for custom claim tests
104 "alt_email": "alt_email@example.com", // for custom claim tests
105 "alt_username": "desired-username", // for custom claim tests
107 json.NewEncoder(w).Encode(struct {
108 AccessToken string `json:"access_token"`
109 TokenType string `json:"token_type"`
110 RefreshToken string `json:"refresh_token"`
111 ExpiresIn int32 `json:"expires_in"`
112 IDToken string `json:"id_token"`
114 AccessToken: p.ValidAccessToken(),
116 RefreshToken: "test-refresh-token",
118 IDToken: p.fakeToken(idToken),
121 json.NewEncoder(w).Encode(jose.JSONWebKeySet{
122 Keys: []jose.JSONWebKey{
123 {Key: p.key.Public(), Algorithm: string(jose.RS256), KeyID: ""},
127 w.WriteHeader(http.StatusInternalServerError)
129 authhdr := req.Header.Get("Authorization")
130 if _, err := jwt.ParseSigned(strings.TrimPrefix(authhdr, "Bearer ")); err != nil {
131 p.c.Logf("OIDCProvider: bad auth %q", authhdr)
132 w.WriteHeader(http.StatusUnauthorized)
135 json.NewEncoder(w).Encode(map[string]interface{}{
136 "sub": "fake-user-id",
138 "given_name": p.AuthGivenName,
139 "family_name": p.AuthFamilyName,
140 "alt_username": "desired-username",
141 "email": p.AuthEmail,
142 "email_verified": p.AuthEmailVerified,
145 w.WriteHeader(http.StatusNotFound)
149 func (p *OIDCProvider) servePeopleAPI(w http.ResponseWriter, req *http.Request) {
151 p.c.Logf("servePeopleAPI: got req: %s %s %s", req.Method, req.URL, req.Form)
152 w.Header().Set("Content-Type", "application/json")
153 switch req.URL.Path {
154 case "/v1/people/me":
155 if f := req.Form.Get("personFields"); f != "emailAddresses,names" {
156 w.WriteHeader(http.StatusBadRequest)
159 json.NewEncoder(w).Encode(p.PeopleAPIResponse)
161 w.WriteHeader(http.StatusNotFound)
165 func (p *OIDCProvider) fakeToken(payload []byte) string {
166 signer, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.RS256, Key: p.key}, nil)
171 object, err := signer.Sign(payload)
176 t, err := object.CompactSerialize()
181 p.c.Logf("fakeToken(%q) == %q", payload, t)