2021-05-05 05:06:05 +07:00
package handlers
import (
"net/http"
"github.com/ory/fosite"
2021-08-11 08:04:35 +07:00
"github.com/authelia/authelia/v4/internal/middlewares"
2022-03-16 05:55:38 +07:00
"github.com/authelia/authelia/v4/internal/oidc"
2021-05-05 05:06:05 +07:00
)
2022-04-07 07:58:51 +07:00
// OpenIDConnectTokenPOST handles POST requests to the OpenID Connect 1.0 Token endpoint.
//
// https://openid.net/specs/openid-connect-core-1_0.html#TokenEndpoint
func OpenIDConnectTokenPOST ( ctx * middlewares . AutheliaCtx , rw http . ResponseWriter , req * http . Request ) {
2022-03-16 05:55:38 +07:00
var (
requester fosite . AccessRequester
responder fosite . AccessResponder
err error
)
2021-05-05 05:06:05 +07:00
2022-03-16 05:55:38 +07:00
oidcSession := oidc . NewSession ( )
if requester , err = ctx . Providers . OpenIDConnect . Fosite . NewAccessRequest ( ctx , req , oidcSession ) ; err != nil {
rfc := fosite . ErrorToRFC6749Error ( err )
2022-04-12 18:39:15 +07:00
ctx . Logger . Errorf ( "Access Request failed with error: %s" , rfc . GetDescription ( ) )
2022-03-16 05:55:38 +07:00
ctx . Providers . OpenIDConnect . Fosite . WriteAccessError ( rw , requester , err )
2021-05-05 05:06:05 +07:00
return
}
2022-03-16 05:55:38 +07:00
client := requester . GetClient ( )
ctx . Logger . Debugf ( "Access Request with id '%s' on client with id '%s' is being processed" , requester . GetID ( ) , client . GetID ( ) )
2021-05-05 05:06:05 +07:00
// If this is a client_credentials grant, grant all scopes the client is allowed to perform.
2022-03-16 05:55:38 +07:00
if requester . GetGrantTypes ( ) . ExactOne ( "client_credentials" ) {
for _ , scope := range requester . GetRequestedScopes ( ) {
if fosite . HierarchicScopeStrategy ( client . GetScopes ( ) , scope ) {
requester . GrantScope ( scope )
2021-05-05 05:06:05 +07:00
}
}
}
2022-03-16 05:55:38 +07:00
if responder , err = ctx . Providers . OpenIDConnect . Fosite . NewAccessResponse ( ctx , requester ) ; err != nil {
rfc := fosite . ErrorToRFC6749Error ( err )
2022-04-12 18:39:15 +07:00
ctx . Logger . Errorf ( "Access Response for Request with id '%s' failed to be created with error: %s" , requester . GetID ( ) , rfc . GetDescription ( ) )
2022-03-16 05:55:38 +07:00
ctx . Providers . OpenIDConnect . Fosite . WriteAccessError ( rw , requester , err )
2021-05-05 05:06:05 +07:00
return
}
2022-03-16 05:55:38 +07:00
ctx . Logger . Debugf ( "Access Request with id '%s' on client with id '%s' has successfully been processed" , requester . GetID ( ) , client . GetID ( ) )
ctx . Logger . Tracef ( "Access Request with id '%s' on client with id '%s' produced the following claims: %+v" , requester . GetID ( ) , client . GetID ( ) , responder . ToMap ( ) )
ctx . Providers . OpenIDConnect . Fosite . WriteAccessResponse ( rw , requester , responder )
2021-05-05 05:06:05 +07:00
}