Skip to content

Commit 1af4709

Browse files
committed
feat: add oauth-authorization-server discovery endpoint
Signed-off-by: Giovanni Vella <giovanni.vella98@gmail.com> feat: add oauth-authorization-server discovery handler Signed-off-by: Giovanni Vella <giovanni.vella98@gmail.com> feat: add oauth-authorization-server tests for endpoint Signed-off-by: Giovanni Vella <giovanni.vella98@gmail.com>
1 parent 8ab38eb commit 1af4709

3 files changed

Lines changed: 77 additions & 7 deletions

File tree

connector/oidc/oidc_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,17 @@ func setupServer(tok map[string]interface{}, idTokenDesired bool) (*httptest.Ser
862862
})
863863
})
864864

865+
mux.HandleFunc("/.well-known/oauth-authorization-server", func(w http.ResponseWriter, r *http.Request) {
866+
url := fmt.Sprintf("http://%s", r.Host)
867+
868+
json.NewEncoder(w).Encode(&map[string]string{
869+
"issuer": url,
870+
"token_endpoint": fmt.Sprintf("%s/token", url),
871+
"authorization_endpoint": fmt.Sprintf("%s/authorize", url),
872+
"jwks_uri": fmt.Sprintf("%s/keys", url),
873+
})
874+
})
875+
865876
return httptest.NewServer(mux), nil
866877
}
867878

server/handlers.go

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (s *Server) handlePublicKeys(w http.ResponseWriter, r *http.Request) {
7272
w.Write(data)
7373
}
7474

75-
type discovery struct {
75+
type discoveryOIDC struct {
7676
Issuer string `json:"issuer"`
7777
Auth string `json:"authorization_endpoint"`
7878
Token string `json:"token_endpoint"`
@@ -90,8 +90,36 @@ type discovery struct {
9090
Claims []string `json:"claims_supported"`
9191
}
9292

93-
func (s *Server) discoveryHandler() (http.HandlerFunc, error) {
94-
d := s.constructDiscovery()
93+
type discoveryOAuth2 struct {
94+
Issuer string `json:"issuer"`
95+
Auth string `json:"authorization_endpoint"`
96+
Token string `json:"token_endpoint"`
97+
Keys string `json:"jwks_uri"`
98+
DeviceEndpoint string `json:"device_authorization_endpoint,omitempty"`
99+
Introspect string `json:"introspection_endpoint,omitempty"`
100+
GrantTypes []string `json:"grant_types_supported"`
101+
ResponseTypes []string `json:"response_types_supported"`
102+
CodeChallengeAlgs []string `json:"code_challenge_methods_supported,omitempty"`
103+
Scopes []string `json:"scopes_supported,omitempty"`
104+
AuthMethods []string `json:"token_endpoint_auth_methods_supported,omitempty"`
105+
}
106+
107+
type DiscoveryType int
108+
109+
const (
110+
DiscoveryOIDC DiscoveryType = iota
111+
DiscoveryOAuth2
112+
)
113+
114+
func (s *Server) discoveryHandler(t DiscoveryType) (http.HandlerFunc, error) {
115+
var d interface{}
116+
117+
switch t {
118+
case DiscoveryOAuth2:
119+
d = s.constructDiscoveryOAuth2()
120+
default:
121+
d = s.constructDiscoveryOIDC()
122+
}
95123

96124
data, err := json.MarshalIndent(d, "", " ")
97125
if err != nil {
@@ -105,8 +133,8 @@ func (s *Server) discoveryHandler() (http.HandlerFunc, error) {
105133
}), nil
106134
}
107135

108-
func (s *Server) constructDiscovery() discovery {
109-
d := discovery{
136+
func (s *Server) constructDiscoveryOIDC() discoveryOIDC {
137+
d := discoveryOIDC{
110138
Issuer: s.issuerURL.String(),
111139
Auth: s.absURL("/auth"),
112140
Token: s.absURL("/token"),
@@ -134,6 +162,31 @@ func (s *Server) constructDiscovery() discovery {
134162
return d
135163
}
136164

165+
func (s *Server) constructDiscoveryOAuth2() discoveryOAuth2 {
166+
d := discoveryOAuth2{
167+
Issuer: s.issuerURL.String(),
168+
Auth: s.absURL("/auth"),
169+
Token: s.absURL("/token"),
170+
Keys: s.absURL("/keys"),
171+
DeviceEndpoint: s.absURL("/device/code"),
172+
Introspect: s.absURL("/token/introspect"),
173+
CodeChallengeAlgs: []string{codeChallengeMethodS256, codeChallengeMethodPlain},
174+
Scopes: []string{"offline_access"},
175+
AuthMethods: []string{"client_secret_basic", "client_secret_post"},
176+
}
177+
178+
// response_types_supported
179+
for responseType := range s.supportedResponseTypes {
180+
d.ResponseTypes = append(d.ResponseTypes, responseType)
181+
}
182+
sort.Strings(d.ResponseTypes)
183+
184+
// grant_types_supported
185+
d.GrantTypes = s.supportedGrantTypes
186+
187+
return d
188+
}
189+
137190
// handleAuthorization handles the OAuth2 auth endpoint.
138191
func (s *Server) handleAuthorization(w http.ResponseWriter, r *http.Request) {
139192
ctx := r.Context()

server/server.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,11 +452,17 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy)
452452
}
453453
r.NotFoundHandler = http.NotFoundHandler()
454454

455-
discoveryHandler, err := s.discoveryHandler()
455+
oidcHandler, err := s.discoveryHandler(DiscoveryOIDC)
456456
if err != nil {
457457
return nil, err
458458
}
459-
handleWithCORS("/.well-known/openid-configuration", discoveryHandler)
459+
handleWithCORS("/.well-known/openid-configuration", oidcHandler)
460+
461+
oauthHandler, err := s.discoveryHandler(DiscoveryOAuth2)
462+
if err != nil {
463+
return nil, err
464+
}
465+
handleWithCORS("/.well-known/oauth-authorization-server", oauthHandler)
460466
// Handle the root path for the better user experience.
461467
handleWithCORS("/", func(w http.ResponseWriter, r *http.Request) {
462468
_, err := fmt.Fprintf(w, `<!DOCTYPE html>

0 commit comments

Comments
 (0)