Skip to content

Commit

Permalink
complete OIDC Registration Flow
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdarshankumar committed Dec 27, 2022
1 parent 5d54843 commit 2d2f7be
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
36 changes: 34 additions & 2 deletions api/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"github.com/sdslabs/nymeria/log"
"github.com/sdslabs/nymeria/pkg/wrapper/kratos/login"
"github.com/sdslabs/nymeria/pkg/wrapper/kratos/registration"
"github.com/sdslabs/nymeria/pkg/wrapper/kratos/oidc"
)

func HandleOIDCLogin(c *gin.Context) {
log.Logger.Debug("Get Google Login")
log.Logger.Debug("Get OIDC Login")
cookie, flowID, csrf_token, err := login.InitializeLoginFlowWrapper()

if err != nil {
Expand All @@ -25,6 +26,14 @@ func HandleOIDCLogin(c *gin.Context) {
}

func HandleOIDCRegister(c *gin.Context) {
log.Logger.Debug("Get OIDC Registration")
provider := c.Param("provider")
if provider == "" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "provider not found",
})
return
}
cookie, flowID, csrf_token, err := registration.InitializeRegistrationFlowWrapper()

if err != nil {
Expand All @@ -35,6 +44,29 @@ func HandleOIDCRegister(c *gin.Context) {
return
}

c.SetCookie("registration_flow", cookie, 3600, "/", "localhost", false, true)
c.SetCookie("OIDC_registration_flow", cookie, 3600, "/", "localhost", false, true)
//In case we need to separate the flows so setting and getting cookies simultaneously
afterCookie, err := c.Cookie("OIDC_registration_flow")

if err != nil {
log.ErrorLogger("Cookie not found", err)
c.JSON(http.StatusBadRequest, gin.H{
"error": "csrf cookie not found",
})
return
}
session, err := oidc.SubmitOIDCRegistrationFlowWrapper(provider, afterCookie, flowID, csrf_token)

if err != nil {
log.ErrorLogger("Kratos post registration flow failed", err)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "internal server error",
})
return
}
c.SetCookie("sdslabs_session", session, 3600, "/", "localhost", false, true)
c.JSON(http.StatusOK, gin.H{
"status": "created",
})

}
2 changes: 1 addition & 1 deletion pkg/wrapper/kratos/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ func SubmitOIDCRegistrationFlowWrapper(provider string, cookie string, flowID st

responseCookies := r.Header["Set-Cookie"]
return responseCookies[1], nil
}
}

0 comments on commit 2d2f7be

Please sign in to comment.