error handling
This commit is contained in:
parent
6993769f06
commit
f4dac05713
3 changed files with 91 additions and 58 deletions
|
@ -61,37 +61,31 @@ func (s *webServer) verifyRequestClient(r *http.Request) (Client, error) {
|
|||
func (s *webServer) handleToken(w http.ResponseWriter, r *http.Request) {
|
||||
client, err := s.verifyRequestClient(r)
|
||||
if err != nil {
|
||||
RequestError(w, r, err, slog.Default())
|
||||
WriteError(w, r, err, slog.Default())
|
||||
return
|
||||
}
|
||||
|
||||
grantType := oidc.GrantType(r.Form.Get("grant_type"))
|
||||
var handle func(w http.ResponseWriter, r *http.Request, client Client)
|
||||
switch grantType {
|
||||
case oidc.GrantTypeCode:
|
||||
handle = s.handleCodeExchange
|
||||
s.handleCodeExchange(w, r, client)
|
||||
case oidc.GrantTypeRefreshToken:
|
||||
handle = s.handleRefreshToken
|
||||
s.handleRefreshToken(w, r, client)
|
||||
case "":
|
||||
RequestError(w, r, oidc.ErrInvalidRequest().WithDescription("grant_type missing"), slog.Default())
|
||||
return
|
||||
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("grant_type missing"), slog.Default())
|
||||
default:
|
||||
RequestError(w, r, oidc.ErrUnsupportedGrantType().WithDescription("%s not supported", grantType), slog.Default())
|
||||
return
|
||||
WriteError(w, r, unimplementedGrantError(grantType), slog.Default())
|
||||
}
|
||||
|
||||
handle(w, r, client)
|
||||
}
|
||||
|
||||
func (s *webServer) handleCodeExchange(w http.ResponseWriter, r *http.Request, client Client) {
|
||||
request, err := decodeRequest[*oidc.AccessTokenRequest](s.decoder, r.Form)
|
||||
if err != nil {
|
||||
RequestError(w, r, err, s.logger)
|
||||
WriteError(w, r, err, s.logger)
|
||||
return
|
||||
}
|
||||
resp, err := s.server.CodeExchange(r.Context(), newClientRequest(r, request, client))
|
||||
if err != nil {
|
||||
RequestError(w, r, err, s.logger)
|
||||
WriteError(w, r, err, s.logger)
|
||||
return
|
||||
}
|
||||
resp.writeOut(w)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue