Skip to content

Commit

Permalink
complete OIDC Login flow
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdarshankumar committed Dec 27, 2022
1 parent 2d2f7be commit 7cfd236
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
40 changes: 36 additions & 4 deletions api/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ import (
"github.com/gin-gonic/gin"
"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"
"github.com/sdslabs/nymeria/pkg/wrapper/kratos/registration"
)

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

if err != nil {
Expand All @@ -21,7 +28,32 @@ func HandleOIDCLogin(c *gin.Context) {
})
return
}
c.SetCookie("googlelogin_flow", cookie, 3600, "/", "localhost", false, true)
c.SetCookie("OIDC_login_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_login_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.SubmitOIDCLoginFlowWrapper(provider, afterCookie, flowID, csrf_token)

if err != nil {
log.ErrorLogger("Kratos post OIDC login 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": "user logged in via OIDC",
})

}

Expand Down Expand Up @@ -58,15 +90,15 @@ func HandleOIDCRegister(c *gin.Context) {
session, err := oidc.SubmitOIDCRegistrationFlowWrapper(provider, afterCookie, flowID, csrf_token)

if err != nil {
log.ErrorLogger("Kratos post registration flow failed", err)
log.ErrorLogger("Kratos OIDC 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",
"status": "created via OIDC",
})

}
18 changes: 17 additions & 1 deletion pkg/wrapper/kratos/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"os"

client "github.com/ory/kratos-client-go"
client "github.com/ory/client-go"
"github.com/sdslabs/nymeria/config"
)

Expand All @@ -26,3 +26,19 @@ func SubmitOIDCRegistrationFlowWrapper(provider string, cookie string, flowID st
responseCookies := r.Header["Set-Cookie"]
return responseCookies[1], nil
}

func SubmitOIDCLoginFlowWrapper(provider string, cookie string, flowID string, csrfToken string) (string, error) {
submitOIDCLoginFlowBody := client.SubmitSelfServiceLoginFlowBody{SubmitSelfServiceLoginFlowWithOidcMethodBody: client.NewSubmitSelfServiceLoginFlowWithOidcMethodBody("oidc", provider)} // SubmitSelfServiceLoginFlowBody |

submitOIDCLoginFlowBody.SubmitSelfServiceLoginFlowWithOidcMethodBody.SetCsrfToken(csrfToken)

apiClient := client.NewAPIClient(config.KratosClientConfig)
_, r, err := apiClient.V0alpha2Api.SubmitSelfServiceLoginFlow(context.Background()).Flow(flowID).SubmitSelfServiceLoginFlowBody(submitOIDCLoginFlowBody).XSessionToken("").Cookie(cookie).Execute()
if err != nil {
return "", err
}

responseCookies := r.Header["Set-Cookie"]

return responseCookies[1], nil
}
6 changes: 0 additions & 6 deletions pkg/wrapper/kratos/oidc/types.go

This file was deleted.

0 comments on commit 7cfd236

Please sign in to comment.