@@ -27,21 +27,54 @@ const ClientID = "fastly-cli"
27
27
// RedirectURL is the endpoint the auth provider will pass an authorization code to.
28
28
const RedirectURL = "http://localhost:8080/callback"
29
29
30
+ // Starter defines the behaviour for the authentication server.
31
+ type Starter interface {
32
+ // GetResult returns the results channel
33
+ GetResult () chan AuthorizationResult
34
+ // SetAccountEndpoint sets the account endpoint.
35
+ SetAccountEndpoint (endpoint string )
36
+ // SetEndpoint sets the API endpoint.
37
+ SetAPIEndpoint (endpoint string )
38
+ // SetVerifier sets the code verifier.
39
+ SetVerifier (verifier * oidc.S256Verifier )
40
+ // Start starts a local server for handling authentication processing.
41
+ Start () error
42
+ }
43
+
30
44
// Server is a local server responsible for authentication processing.
31
45
type Server struct {
46
+ // APIEndpoint is the API endpoint.
47
+ APIEndpoint string
32
48
// AccountEndpoint is the accounts endpoint.
33
49
AccountEndpoint string
34
- // HTTPClient is a HTTP client used to call the API to exchange the access
35
- // token for a session token.
50
+ // HTTPClient is a HTTP client used to call the API to exchange the access token for a session token.
36
51
HTTPClient api.HTTPClient
37
52
// Result is a channel that reports the result of authorization.
38
53
Result chan AuthorizationResult
39
54
// Router is an HTTP request multiplexer.
40
55
Router * http.ServeMux
41
- // Verifier represents an OAuth PKCE code verifier that uses the S256 challenge method
56
+ // Verifier represents an OAuth PKCE code verifier that uses the S256 challenge method.
42
57
Verifier * oidc.S256Verifier
43
- // APIEndpoint is the API endpoint.
44
- APIEndpoint string
58
+ }
59
+
60
+ // GetResult returns the result channel.
61
+ func (s Server ) GetResult () chan AuthorizationResult {
62
+ return s .Result
63
+ }
64
+
65
+ // SetAccountEndpoint sets the account endpoint.
66
+ func (s * Server ) SetAccountEndpoint (endpoint string ) {
67
+ s .AccountEndpoint = endpoint
68
+ }
69
+
70
+ // SetAPIEndpoint sets the API endpoint.
71
+ func (s * Server ) SetAPIEndpoint (endpoint string ) {
72
+ s .APIEndpoint = endpoint
73
+ }
74
+
75
+ // SetVerifier sets the code verifier endpoint.
76
+ func (s * Server ) SetVerifier (verifier * oidc.S256Verifier ) {
77
+ s .Verifier = verifier
45
78
}
46
79
47
80
// Start starts a local server for handling authentication processing.
@@ -63,12 +96,8 @@ func (s *Server) Start() error {
63
96
return nil
64
97
}
65
98
66
- // Routes configures the callback handler.
67
- func (s * Server ) Routes () {
68
- s .Router .HandleFunc ("/callback" , s .handleCallback ())
69
- }
70
-
71
- func (s * Server ) handleCallback () http.HandlerFunc {
99
+ // HandleCallback processes the callback from the authentication service.
100
+ func (s * Server ) HandleCallback () http.HandlerFunc {
72
101
return func (w http.ResponseWriter , r * http.Request ) {
73
102
authorizationCode := r .URL .Query ().Get ("code" )
74
103
if authorizationCode == "" {
@@ -153,6 +182,11 @@ type AuthorizationResult struct {
153
182
SessionToken string
154
183
}
155
184
185
+ // GenVerifier creates a code verifier.
186
+ func GenVerifier () (* oidc.S256Verifier , error ) {
187
+ return oidc .NewCodeVerifier ()
188
+ }
189
+
156
190
// GenURL constructs the required authorization_endpoint path.
157
191
func GenURL (accountEndpoint , apiEndpoint string , verifier * oidc.S256Verifier ) (string , error ) {
158
192
challenge , err := oidc .CreateCodeChallenge (verifier )
0 commit comments