mobility-accounts/oidc-provider/endpoints_token.go

42 lines
1.0 KiB
Go

package op
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/ory/fosite/handler/openid"
)
func (op *OIDCHandler) TokenEndpoint(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
namespace := mux.Vars(req)["namespace"]
provider := op.NamespaceProviders[namespace]
ctx := req.Context()
mySessionData := openid.NewDefaultSession()
accessRequest, err := provider.NewAccessRequest(ctx, req, mySessionData)
if err != nil {
fmt.Printf("Error occurred in NewAccessRequest: %+v", err)
provider.WriteAccessError(w, accessRequest, err)
return
}
if accessRequest.GetGrantTypes().ExactOne("client_credentials") {
for _, scope := range accessRequest.GetRequestedScopes() {
accessRequest.GrantScope(scope)
}
}
response, err := provider.NewAccessResponse(ctx, accessRequest)
if err != nil {
fmt.Printf("Error occurred in NewAccessResponse: %+v", err)
provider.WriteAccessError(w, accessRequest, err)
return
}
provider.WriteAccessResponse(w, accessRequest, response)
}