mirror of
https://github.com/janishutz/oidclogin.git
synced 2026-10-08 17:26:20 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
da7dd13e2a |
No files matched your search
+10
-4
@@ -10,7 +10,7 @@ import (
|
|||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoginHandler(c *gin.Context) {
|
func loginHandler(c *gin.Context) {
|
||||||
// Set up user session
|
// Set up user session
|
||||||
state := rand.Text()
|
state := rand.Text()
|
||||||
nonce := rand.Text()
|
nonce := rand.Text()
|
||||||
@@ -27,7 +27,7 @@ func LoginHandler(c *gin.Context) {
|
|||||||
c.Redirect(301, config.AuthCodeURL(state, oidc.Nonce(nonce), oauth2.S256ChallengeOption(codeVerifier)))
|
c.Redirect(301, config.AuthCodeURL(state, oidc.Nonce(nonce), oauth2.S256ChallengeOption(codeVerifier)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func CallbackHandler(c *gin.Context) {
|
func callbackHandler(c *gin.Context) {
|
||||||
session := sessions.Default(c)
|
session := sessions.Default(c)
|
||||||
state := session.Get("jhid_oauth_state")
|
state := session.Get("jhid_oauth_state")
|
||||||
nonce := session.Get("jhid_oauth_nonce")
|
nonce := session.Get("jhid_oauth_nonce")
|
||||||
@@ -83,14 +83,20 @@ func CallbackHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
if err := idToken.Claims(&claims); err != nil {
|
if err := idToken.Claims(&claims); err != nil {
|
||||||
log.Println("Token claims generation failed", err)
|
log.Println("Token claims generation failed", err)
|
||||||
c.AbortWithStatus(500)
|
c.HTML(500, "oidcerror.tmpl", gin.H{
|
||||||
|
"error": "ERR_AUTH",
|
||||||
|
})
|
||||||
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify NONCE
|
// Verify NONCE
|
||||||
if nonce != claims.Nonce {
|
if nonce != claims.Nonce {
|
||||||
log.Println("Token verificcation failed", err)
|
log.Println("Token verificcation failed", err)
|
||||||
c.AbortWithStatus(500)
|
c.HTML(500, "oidcerror.tmpl", gin.H{
|
||||||
|
"error": "ERR_AUTH",
|
||||||
|
})
|
||||||
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ var (
|
|||||||
defaultRedirect string
|
defaultRedirect string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Configure and set up the login SDK.
|
||||||
func Configure(r *gin.Engine, app_url string, default_redirect string, stubs_on_unconfigured bool) {
|
func Configure(r *gin.Engine, app_url string, default_redirect string, stubs_on_unconfigured bool) {
|
||||||
issuer := os.Getenv("OIDC_ISSUER")
|
issuer := os.Getenv("OIDC_ISSUER")
|
||||||
clientID := os.Getenv("OIDC_CLIENT_ID")
|
clientID := os.Getenv("OIDC_CLIENT_ID")
|
||||||
@@ -58,8 +59,22 @@ func Configure(r *gin.Engine, app_url string, default_redirect string, stubs_on_
|
|||||||
Scopes: []string{oidc.ScopeOpenID, "email", "profile"},
|
Scopes: []string{oidc.ScopeOpenID, "email", "profile"},
|
||||||
}
|
}
|
||||||
|
|
||||||
r.GET("/auth/v2/login", LoginHandler)
|
r.GET("/auth/v2/login", loginHandler)
|
||||||
r.GET("/auth/v2/verify", CallbackHandler)
|
r.GET("/auth/v2/verify", callbackHandler)
|
||||||
|
|
||||||
log.Println("[JHID] Configured successfully")
|
log.Println("[JHID] Configured successfully")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ErrorHandler() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
c.Next()
|
||||||
|
|
||||||
|
if len(c.Errors) > 0 {
|
||||||
|
log.Println("Error during route:", c.Errors.Last().Err)
|
||||||
|
|
||||||
|
c.JSON(500, gin.H{
|
||||||
|
"error": "Internal Server Error",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user