2021-05-04 22:06:05 +00:00
package handlers
import (
"net/http"
"github.com/ory/fosite"
2021-08-11 01:04:35 +00:00
"github.com/authelia/authelia/v4/internal/middlewares"
2022-03-15 22:55:38 +00:00
"github.com/authelia/authelia/v4/internal/oidc"
2021-05-04 22:06:05 +00:00
)
func oidcToken ( ctx * middlewares . AutheliaCtx , rw http . ResponseWriter , req * http . Request ) {
2022-03-15 22:55:38 +00:00
var (
requester fosite . AccessRequester
responder fosite . AccessResponder
err error
)
2021-05-04 22:06:05 +00:00
2022-03-15 22:55:38 +00:00
oidcSession := oidc . NewSession ( )
if requester , err = ctx . Providers . OpenIDConnect . Fosite . NewAccessRequest ( ctx , req , oidcSession ) ; err != nil {
rfc := fosite . ErrorToRFC6749Error ( err )
ctx . Logger . Errorf ( "Access Request failed with error: %+v" , rfc )
ctx . Providers . OpenIDConnect . Fosite . WriteAccessError ( rw , requester , err )
2021-05-04 22:06:05 +00:00
return
}
2022-03-15 22:55:38 +00: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-04 22:06:05 +00:00
// If this is a client_credentials grant, grant all scopes the client is allowed to perform.
2022-03-15 22:55:38 +00:00
if requester . GetGrantTypes ( ) . ExactOne ( "client_credentials" ) {
for _ , scope := range requester . GetRequestedScopes ( ) {
if fosite . HierarchicScopeStrategy ( client . GetScopes ( ) , scope ) {
requester . GrantScope ( scope )
2021-05-04 22:06:05 +00:00
}
}
}
2022-03-15 22:55:38 +00:00
if responder , err = ctx . Providers . OpenIDConnect . Fosite . NewAccessResponse ( ctx , requester ) ; err != nil {
rfc := fosite . ErrorToRFC6749Error ( err )
ctx . Logger . Errorf ( "Access Response for Request with id '%s' failed to be created with error: %+v" , requester . GetID ( ) , rfc )
ctx . Providers . OpenIDConnect . Fosite . WriteAccessError ( rw , requester , err )
2021-05-04 22:06:05 +00:00
return
}
2022-03-15 22:55:38 +00: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-04 22:06:05 +00:00
}