-
Notifications
You must be signed in to change notification settings - Fork 155
/
fake_auth.go
145 lines (124 loc) · 4.04 KB
/
fake_auth.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 test
import (
"context"
"errors"
"net/http"
"golang.org/x/oauth2"
apiv1 "k8c.io/kubermatic/v2/pkg/api/v1"
"k8c.io/kubermatic/v2/pkg/handler/auth"
)
const (
// AuthorizationCode represents a shared secret used by IssuerVerifier
// TODO: consider injecting it into IssuerVerifier
AuthorizationCode = "fakeCode"
// IDToken represents a shared fake token
IDToken = "fakeTokenId"
IDViewerToken = "fakeViewerTokenId"
refreshToken = "fakeRefreshToken"
tokenURL = "url:tokenURL"
// IssuerURL holds test issuer URL
IssuerURL = "url://dex"
// IssuerClientID holds test issuer client ID
IssuerClientID = "kubermatic"
// IssuerClientSecret holds test issuer client secret
IssuerClientSecret = "secret"
issuerRedirectURL = "/api/v1/kubeconfig"
)
var _ auth.OIDCIssuerVerifier = &IssuerVerifier{}
var _ auth.TokenExtractorVerifier = &IssuerVerifier{}
// OicdProvider is a test stub that mocks *oidc.Provider
type OicdProvider struct {
authURL string
tokenURL string
}
// NewFakeOIDCClient returns fake OIDC issuer and verifier
func NewFakeOIDCClient(user apiv1.User) *IssuerVerifier {
return &IssuerVerifier{
user: user,
issuer: IssuerURL,
clientID: IssuerClientID,
clientSecret: IssuerClientSecret,
redirectURI: issuerRedirectURL,
provider: &OicdProvider{
authURL: IssuerURL,
tokenURL: tokenURL,
},
}
}
// Endpoint returns the OAuth2 auth and token endpoints for the given provider.
func (p *OicdProvider) Endpoint() oauth2.Endpoint {
return oauth2.Endpoint{AuthURL: p.authURL, TokenURL: p.tokenURL}
}
// IssuerVerifier is a test stub that mocks OIDC responses
type IssuerVerifier struct {
user apiv1.User
issuer string
clientID string
clientSecret string
redirectURI string
provider *OicdProvider
}
// Extractor knows how to extract the ID token from the request
func (o *IssuerVerifier) Extract(_ *http.Request) (string, error) {
return IDToken, nil
}
// AuthCodeURL returns a URL to OpenID provider's consent page
func (o *IssuerVerifier) AuthCodeURL(state string, offlineAsScope bool, scopes ...string) string {
oauth2Config := o.oauth2Config(scopes...)
options := oauth2.AccessTypeOnline
if !offlineAsScope {
options = oauth2.AccessTypeOffline
}
return oauth2Config.AuthCodeURL(state, options)
}
// oauth2Config return a oauth2Config
func (o *IssuerVerifier) oauth2Config(scopes ...string) *oauth2.Config {
return &oauth2.Config{
ClientID: o.clientID,
ClientSecret: o.clientSecret,
Endpoint: o.provider.Endpoint(),
Scopes: scopes,
RedirectURL: o.redirectURI,
}
}
// Exchange converts an authorization code into a token.
func (o *IssuerVerifier) Exchange(ctx context.Context, code string) (auth.OIDCToken, error) {
if code != AuthorizationCode {
return auth.OIDCToken{}, errors.New("incorrect code")
}
return auth.OIDCToken{
IDToken: IDToken,
RefreshToken: refreshToken,
}, nil
}
// Verify parses a raw ID Token, verifies it's been signed by the provider, performs
// any additional checks depending on the Config, and returns the payload as TokenClaims.
func (o *IssuerVerifier) Verify(ctx context.Context, token string) (auth.TokenClaims, error) {
if o == nil {
return auth.TokenClaims{}, nil
}
if ctx == nil {
return auth.TokenClaims{}, nil
}
if token != IDToken {
return auth.TokenClaims{}, errors.New("incorrect code")
}
return auth.TokenClaims{
Email: o.user.Email,
Subject: o.user.Email,
Name: o.user.Name,
Groups: []string{},
}, nil
}