-
Notifications
You must be signed in to change notification settings - Fork 5
/
oauth_authenticator.go
196 lines (173 loc) · 6.14 KB
/
oauth_authenticator.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
package auth
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"os"
"time"
"github.com/UiPath/uipathcli/cache"
)
// The OAuthAuthenticator triggers the oauth authorization code flow with proof key for code exchange (PKCE).
//
// The user can login to the UiPath platform using the browser. In case the user interface is available,
// the browser is automatically launched and the oauth flow will be initiated.
// The CLI will open up a port on localhost waiting for the cloud.uipath.com platform to redirect back
// for handing over the authorization code which will be exchanged for a JWT bearer token using the
// token-endpoint from identity.
//
// There is no need to store any long-term credentials.
type OAuthAuthenticator struct {
cache cache.Cache
browserLauncher BrowserLauncher
}
func (a OAuthAuthenticator) Auth(ctx AuthenticatorContext) AuthenticatorResult {
if !a.enabled(ctx) {
return *AuthenticatorSuccess(ctx.Request.Header, ctx.Config)
}
config, err := a.getConfig(ctx)
if err != nil {
return *AuthenticatorError(fmt.Errorf("Invalid oauth authenticator configuration: %w", err))
}
token, err := a.retrieveToken(config.IdentityUri, *config, ctx.Insecure)
if err != nil {
return *AuthenticatorError(fmt.Errorf("Error retrieving access token: %w", err))
}
ctx.Request.Header["Authorization"] = "Bearer " + token
return *AuthenticatorSuccess(ctx.Request.Header, ctx.Config)
}
func (a OAuthAuthenticator) retrieveToken(identityBaseUri url.URL, config oauthAuthenticatorConfig, insecure bool) (string, error) {
cacheKey := fmt.Sprintf("oauthtoken|%s|%s|%s|%s", identityBaseUri.Scheme, identityBaseUri.Hostname(), config.ClientId, config.Scopes)
token, _ := a.cache.Get(cacheKey)
if token != "" {
return token, nil
}
secretGenerator := newSecretGenerator()
codeVerifier, codeChallenge := secretGenerator.GeneratePkce()
state := secretGenerator.GenerateState()
code, err := a.login(identityBaseUri, config, state, codeChallenge)
if err != nil {
return "", err
}
identityClient := newIdentityClient(a.cache)
tokenRequest := newAuthorizationCodeTokenRequest(
identityBaseUri,
config.ClientId,
code,
codeVerifier,
config.RedirectUrl.String(),
insecure)
tokenResponse, err := identityClient.GetToken(*tokenRequest)
if err != nil {
return "", err
}
a.cache.Set(cacheKey, tokenResponse.AccessToken, tokenResponse.ExpiresIn)
return tokenResponse.AccessToken, nil
}
func (a OAuthAuthenticator) login(identityBaseUri url.URL, config oauthAuthenticatorConfig, state string, codeChallenge string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
var code string
var err error
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
code = query.Get("code")
if code == "" {
err = fmt.Errorf("Could not find query string 'code' in redirect_url")
a.writeErrorPage(w, err)
} else if query.Get("state") != state {
err = fmt.Errorf("The query string 'state' in the redirect_url did not match")
a.writeErrorPage(w, err)
} else {
a.writeHtmlPage(w, LOGGED_IN_PAGE_HTML)
}
cancel()
})
listener, err := net.Listen("tcp", config.RedirectUrl.Host)
if err != nil {
return "", fmt.Errorf("Error starting listener on address %s and wait for oauth redirect: %w", config.RedirectUrl.Host, err)
}
defer listener.Close()
server := &http.Server{
Handler: mux,
ReadHeaderTimeout: 30 * time.Second,
}
defer server.Close()
go func(listener net.Listener) {
listenErr := server.Serve(listener)
if listenErr != nil {
err = fmt.Errorf("Error starting server on address %s and wait for oauth redirect: %w", config.RedirectUrl.Host, listenErr)
}
cancel()
}(listener)
port := listener.Addr().(*net.TCPAddr).Port
redirectUri := fmt.Sprintf("%s://%s:%d", config.RedirectUrl.Scheme, config.RedirectUrl.Hostname(), port)
loginUrl := fmt.Sprintf("%s/connect/authorize?response_type=code&client_id=%s&redirect_uri=%s&scope=%s&code_challenge=%s&code_challenge_method=S256&state=%s",
identityBaseUri.String(),
url.QueryEscape(config.ClientId),
url.QueryEscape(redirectUri),
url.QueryEscape(config.Scopes),
url.QueryEscape(codeChallenge),
url.QueryEscape(state))
go func(url string) {
err := a.browserLauncher.Open(url)
if err != nil {
a.showBrowserLink(url)
}
}(loginUrl)
<-ctx.Done()
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return "", fmt.Errorf("OAuth Login expired")
}
if err != nil {
return "", err
}
return code, nil
}
func (a OAuthAuthenticator) enabled(ctx AuthenticatorContext) bool {
return ctx.Config["clientId"] != nil && ctx.Config["redirectUri"] != nil && ctx.Config["scopes"] != nil
}
func (a OAuthAuthenticator) getConfig(ctx AuthenticatorContext) (*oauthAuthenticatorConfig, error) {
clientId, err := a.parseRequiredString(ctx.Config, "clientId")
if err != nil {
return nil, err
}
redirectUri, err := a.parseRequiredString(ctx.Config, "redirectUri")
if err != nil {
return nil, err
}
parsedRedirectUri, err := url.Parse(redirectUri)
if err != nil {
return nil, err
}
scopes, err := a.parseRequiredString(ctx.Config, "scopes")
if err != nil {
return nil, err
}
return newOAuthAuthenticatorConfig(clientId, *parsedRedirectUri, scopes, ctx.IdentityUri), nil
}
func (a OAuthAuthenticator) parseRequiredString(config map[string]interface{}, name string) (string, error) {
value := config[name]
result, valid := value.(string)
if !valid || result == "" {
return "", fmt.Errorf("Invalid value for %s: '%v'", name, value)
}
return result, nil
}
func (a OAuthAuthenticator) showBrowserLink(url string) {
fmt.Fprintln(os.Stderr, "Go to URL and perform login:\n"+url)
}
func (a OAuthAuthenticator) writeErrorPage(w http.ResponseWriter, err error) {
w.Header().Add("content-type", "text/html")
_, _ = w.Write([]byte(err.Error()))
}
func (a OAuthAuthenticator) writeHtmlPage(w http.ResponseWriter, html string) {
w.Header().Add("content-type", "text/html")
_, _ = w.Write([]byte(html))
}
func NewOAuthAuthenticator(cache cache.Cache, browserLauncher BrowserLauncher) *OAuthAuthenticator {
return &OAuthAuthenticator{cache, browserLauncher}
}