-
Notifications
You must be signed in to change notification settings - Fork 3
/
oidc_client.go
244 lines (207 loc) · 7.42 KB
/
oidc_client.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2021 Monoskope Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package auth
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/cenkalti/backoff"
"github.com/coreos/go-oidc"
grpcUtil "github.com/finleap-connect/monoskope/pkg/grpc"
"github.com/finleap-connect/monoskope/pkg/jwt"
"github.com/finleap-connect/monoskope/pkg/logger"
"github.com/finleap-connect/monoskope/pkg/util"
"golang.org/x/oauth2"
)
type ClientConfig struct {
IdentityProvider string // URL of the upstream identity provider
Scopes []string
OfflineAsScope bool
Nonce string
ClientId string
ClientSecret string
RedirectURIs []string
}
// Implements an OIDC client which authenticates to an upstream IDP
type Client struct {
config *ClientConfig
httpClient *http.Client
provider *oidc.Provider
upstreamVerifier *oidc.IDTokenVerifier
log logger.Logger
}
func NewClient(config *ClientConfig) *Client {
n := &Client{
config: config,
httpClient: http.DefaultClient,
log: logger.WithName("auth"),
}
n.log.Info("Auth handler configured.",
"Scopes",
n.config.Scopes,
"RedirectURIs",
n.config.RedirectURIs,
)
return n
}
func (n *Client) SetupOIDC(ctx context.Context) error {
ctx = oidc.ClientContext(ctx, n.httpClient)
// Using an exponential backoff to avoid issues in development environments
backoffParams := backoff.NewExponentialBackOff()
backoffParams.MaxElapsedTime = time.Second * 10
err := backoff.Retry(func() error {
var err error
n.provider, err = oidc.NewProvider(ctx, n.config.IdentityProvider)
return err
}, backoffParams)
if err != nil {
return fmt.Errorf("failed to query provider %q: %v", n.config.IdentityProvider, err)
}
// What scopes does a provider support?
var scopes struct {
// See: https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata
Supported []string `json:"scopes_supported"`
}
if err := n.provider.Claims(&scopes); err != nil {
return fmt.Errorf("failed to parse provider scopes_supported: %v", err)
}
if len(scopes.Supported) == 0 {
// scopes_supported is a "RECOMMENDED" discovery claim, not a required
// one. If missing, assume that the provider follows the spec and has
// an "offline_access" scope.
n.config.OfflineAsScope = true
} else {
// See if scopes_supported has the "offline_access" scope.
n.config.OfflineAsScope = func() bool {
for _, scope := range scopes.Supported {
if scope == oidc.ScopeOfflineAccess {
return true
}
}
return false
}()
}
n.upstreamVerifier = n.provider.Verifier(&oidc.Config{ClientID: n.config.ClientId})
n.log.Info("Connected to auth provider successful.", "AuthURL", n.provider.Endpoint().AuthURL, "TokenURL", n.provider.Endpoint().TokenURL, "AuthStyle", n.provider.Endpoint().AuthStyle, "SupportedScopes", scopes.Supported)
return nil
}
func (n *Client) getOauth2Config(scopes []string, redirectURL string) *oauth2.Config {
return &oauth2.Config{
ClientID: n.config.ClientId,
ClientSecret: n.config.ClientSecret,
Endpoint: n.provider.Endpoint(),
Scopes: scopes,
RedirectURL: redirectURL,
}
}
func (n *Client) clientContext(ctx context.Context) context.Context {
return oidc.ClientContext(ctx, n.httpClient)
}
func getClaims(idToken *oidc.IDToken) (*jwt.StandardClaims, error) {
claims := &jwt.StandardClaims{}
if err := idToken.Claims(claims); err != nil {
return nil, fmt.Errorf("failed to parse claims: %v", err)
}
if !claims.EmailVerified {
return nil, fmt.Errorf("email (%q) in returned claims was not verified", claims.Email)
}
return claims, nil
}
// exchange exchanges the auth code with a token of the upstream IDP
func (n *Client) exchange(ctx context.Context, code, redirectURL string) (*oauth2.Token, error) {
n.log.Info("Exchanging auth code for token...")
return n.getOauth2Config(nil, redirectURL).Exchange(n.clientContext(ctx), code)
}
func (n *Client) redirectUrlAllowed(callBackUrl string) bool {
for _, validUrl := range n.config.RedirectURIs {
if strings.EqualFold(strings.ToLower(validUrl), strings.ToLower(callBackUrl)) {
return true
}
}
return false
}
func (n *Client) verifyStateAndClaims(ctx context.Context, token *oauth2.Token, encodedState string) (*jwt.StandardClaims, error) {
n.log.Info("Verifying state and claims...")
if !token.Valid() {
return nil, fmt.Errorf("failed to verify ID token")
}
rawIDToken := token.Extra("id_token").(string)
idToken, err := n.upstreamVerifier.Verify(ctx, rawIDToken)
if err != nil {
return nil, fmt.Errorf("failed to verify ID token: %v", err)
}
if idToken.Nonce != util.HashString(encodedState+n.config.Nonce) {
return nil, fmt.Errorf("invalid id_token nonce")
}
state, err := DecodeState(encodedState)
if err != nil {
return nil, fmt.Errorf("failed to decode state")
}
if !state.IsValid() {
return nil, grpcUtil.ErrInvalidArgument("url is invalid")
}
claims, err := getClaims(idToken)
if err != nil {
return nil, err
}
n.log.Info("Token verified successfully.", "User", claims.Email, "TokenType", token.TokenType)
return claims, nil
}
// Exchange exchanges the auth code with a token of the upstream IDP and verifies the claims
func (n *Client) Exchange(ctx context.Context, code, state string) (*jwt.StandardClaims, error) {
decodedState, err := DecodeState(state)
if err != nil {
return nil, fmt.Errorf("failed to decode state")
}
if !decodedState.IsValid() {
return nil, grpcUtil.ErrInvalidArgument("url is invalid")
}
upstreamToken, err := n.exchange(ctx, code, decodedState.Callback)
if err != nil {
return nil, err
}
n.log.V(logger.DebugLevel).Info("Token received in exchange for auth code.", "Token", upstreamToken)
upstreamClaims, err := n.verifyStateAndClaims(ctx, upstreamToken, state)
if err != nil {
return nil, err
}
n.log.V(logger.DebugLevel).Info("Claims verified.", "Claims", upstreamClaims)
return upstreamClaims, nil
}
// AuthCodeURL returns a URL to OAuth 2.0 provider's consent page that asks for permissions for the required scopes explicitly.
func (n *Client) GetAuthCodeURL(redirectUrl string) (string, string, error) {
if !n.redirectUrlAllowed(redirectUrl) {
return "", "", errors.New("callback url not allowed")
}
// Encode state and calculate nonce
encoded, err := (&State{Callback: redirectUrl}).Encode()
if err != nil {
return "", "", err
}
nonce := util.HashString(encoded + n.config.Nonce)
// Construct authCodeURL
var authCodeURL string
var scopes []string
scopes = append(scopes, n.config.Scopes...)
if n.config.OfflineAsScope {
scopes = append(n.config.Scopes, oidc.ScopeOfflineAccess)
authCodeURL = n.getOauth2Config(scopes, redirectUrl).AuthCodeURL(encoded, oauth2.SetAuthURLParam("prompt", "login"), oidc.Nonce(nonce))
} else {
authCodeURL = n.getOauth2Config(scopes, redirectUrl).AuthCodeURL(encoded, oauth2.SetAuthURLParam("prompt", "login"), oidc.Nonce(nonce), oauth2.AccessTypeOffline)
}
return authCodeURL, encoded, nil
}