mirror of
https://github.com/janishutz/oidclogin.git
synced 2026-10-08 17:26:20 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6cb582a762 | ||
|
|
e241d90f42 | ||
|
|
59f635f01e | ||
|
|
d9ca3abb6c | ||
|
|
bc4abcf5e3 | ||
|
|
92182271b0 | ||
|
|
fa9cc4fbcb | ||
|
|
2ae92f8191 | ||
|
|
a9245fd20f | ||
|
|
996f4959fb | ||
|
|
6fb820d6af |
No files matched your search
+22
@@ -102,6 +102,8 @@ func callbackHandler(c *gin.Context) {
|
||||
|
||||
if userFunc != nil {
|
||||
userFunc(claims.Uid, claims.Name, claims.Email)
|
||||
} else {
|
||||
log.Println("[JHID] WARNING: No user function defined")
|
||||
}
|
||||
|
||||
// Clear session data of oauth related state
|
||||
@@ -112,6 +114,7 @@ func callbackHandler(c *gin.Context) {
|
||||
session.Delete("redirect")
|
||||
|
||||
session.Set("jhid_auth", true)
|
||||
session.Set("jhid_uid", claims.Uid)
|
||||
session.Save()
|
||||
|
||||
if redir != nil {
|
||||
@@ -121,10 +124,29 @@ func callbackHandler(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func logoutHandler(c *gin.Context) {
|
||||
session := sessions.Default(c)
|
||||
session.Clear()
|
||||
session.Save()
|
||||
redir := ""
|
||||
if c.Query("returnTo") != "" {
|
||||
redir = c.Query("returnTo")
|
||||
}
|
||||
if redir != "" {
|
||||
c.Redirect(307, redir)
|
||||
} else {
|
||||
c.Redirect(307, defaultRedirect)
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure that a user is currently logged in.
|
||||
// If redirectFail is set, then if not, the user is redirected.
|
||||
func EnsureLogin(redirectFail bool) func(c *gin.Context) {
|
||||
return (func(c *gin.Context) {
|
||||
session := sessions.Default(c)
|
||||
if session.Get("jhid_auth") == true {
|
||||
// Set the UID on the context
|
||||
c.Set("uid", session.Get("jhid_uid").(string))
|
||||
c.Next()
|
||||
} else {
|
||||
if redirectFail {
|
||||
|
||||
@@ -20,18 +20,30 @@ func createRandomString(n int) (string, error) {
|
||||
return base64.URLEncoding.EncodeToString(s), nil
|
||||
}
|
||||
|
||||
type UserFunc func(userid string, name string, email string)
|
||||
|
||||
var (
|
||||
config oauth2.Config
|
||||
userFunc func(userid string, name string, email string)
|
||||
userFunc UserFunc
|
||||
verifier oidc.IDTokenVerifier
|
||||
defaultRedirect string
|
||||
)
|
||||
|
||||
// Configure and set up the login SDK.
|
||||
func Configure(r *gin.Engine, app_url string, default_redirect string, stubs_on_unconfigured bool) {
|
||||
// Wraps the normal configure function, but also gives you access to change the User Function, which is called upon login.
|
||||
// It is used to create or update a user.
|
||||
// The check middleware may be nil, in which case a default is used. Otherwise should be a valid gin middleware, calling c.Next() if okay to proceed.
|
||||
func ConfigureFull(r *gin.Engine, app_url string, default_redirect string, user_function UserFunc, stubs_on_unconfigured bool, check_middleware *func(c *gin.Context)) {
|
||||
userFunc = user_function
|
||||
Configure(r, app_url, default_redirect, stubs_on_unconfigured, check_middleware)
|
||||
}
|
||||
|
||||
// Configure and set up the login SDK
|
||||
// The check middleware may be nil, in which case a default is used. Otherwise should be a valid gin middleware, calling c.Next() if okay to proceed.
|
||||
func Configure(r *gin.Engine, app_url string, default_redirect string, stubs_on_unconfigured bool, check_middleware *func(c *gin.Context)) {
|
||||
issuer := os.Getenv("OIDC_ISSUER")
|
||||
clientID := os.Getenv("OIDC_CLIENT_ID")
|
||||
clientSecret := os.Getenv("OIDC_CLIENT_SECRET")
|
||||
defaultRedirect = default_redirect
|
||||
|
||||
if issuer == "" || clientID == "" || clientSecret == "" {
|
||||
if stubs_on_unconfigured {
|
||||
@@ -45,7 +57,6 @@ func Configure(r *gin.Engine, app_url string, default_redirect string, stubs_on_
|
||||
|
||||
provider, err := oidc.NewProvider(context.Background(), issuer)
|
||||
verifier = *provider.Verifier(&oidc.Config{ClientID: clientID})
|
||||
defaultRedirect = default_redirect
|
||||
|
||||
if err != nil {
|
||||
log.Fatal("[JHID] Provider resolution failed with error", err)
|
||||
@@ -61,6 +72,13 @@ func Configure(r *gin.Engine, app_url string, default_redirect string, stubs_on_
|
||||
|
||||
r.GET("/auth/v2/login", loginHandler)
|
||||
r.GET("/auth/v2/verify", callbackHandler)
|
||||
check_finalizer := func(ctx *gin.Context) { ctx.JSON(200, gin.H{"success": "true"}) }
|
||||
if check_middleware == nil {
|
||||
r.GET("/auth/v2/check", EnsureLogin(false), check_finalizer)
|
||||
} else {
|
||||
r.GET("/auth/v2/check", EnsureLogin(false), *check_middleware, check_finalizer)
|
||||
}
|
||||
r.GET("/auth/v2/logout", logoutHandler)
|
||||
|
||||
log.Println("[JHID] Configured successfully")
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<!-- NOTE: You should definitely host this yourself! (and you can adjust the style that way, too!) -->
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<!-- NOTE: You should definitely host this yourself! (and you can adjust the style that way, too!) -->
|
||||
|
||||
@@ -36,6 +36,7 @@ a {
|
||||
padding: 10px;
|
||||
border-radius: 10px;
|
||||
cursor: pointer;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.error-msg {
|
||||
|
||||
@@ -1,22 +1,35 @@
|
||||
package oidclogin
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func startStubs(r *gin.Engine) {
|
||||
r.GET("/auth/v2/login", func(ctx *gin.Context) {
|
||||
redir := ctx.Query("returnTo")
|
||||
if redir != "" {
|
||||
ctx.Redirect(307, redir)
|
||||
} else {
|
||||
ctx.Redirect(307, defaultRedirect)
|
||||
}
|
||||
})
|
||||
r.GET("/auth/v2/verify", func(ctx *gin.Context) {
|
||||
redir := ctx.Query("returnTo")
|
||||
if redir != "" {
|
||||
ctx.Redirect(307, redir)
|
||||
} else {
|
||||
ctx.Redirect(307, defaultRedirect)
|
||||
}
|
||||
})
|
||||
r.GET("/auth/v2/login", stubsHandler)
|
||||
r.GET("/auth/v2/verify", stubsHandler)
|
||||
r.GET("/auth/v2/check", EnsureLogin(false), func(ctx *gin.Context) { ctx.JSON(200, gin.H{"success": "true"}) })
|
||||
r.GET("/auth/v2/logout", logoutHandler)
|
||||
}
|
||||
|
||||
func stubsHandler(c *gin.Context) {
|
||||
redir := c.Query("returnTo")
|
||||
session := sessions.Default(c)
|
||||
if userFunc != nil {
|
||||
userFunc("stubs", "Stubs User", "example@example.com")
|
||||
} else {
|
||||
log.Println("[JHID] WARNING: No user function defined")
|
||||
}
|
||||
session.Set("jhid_auth", true)
|
||||
session.Set("jhid_uid", "stubs")
|
||||
session.Save()
|
||||
if redir != "" {
|
||||
log.Println("[JHID] Redirecting to ", redir)
|
||||
c.Redirect(307, redir)
|
||||
} else {
|
||||
log.Println("[JHID] Redirecting to ", defaultRedirect, " (default redirect)")
|
||||
c.Redirect(307, defaultRedirect)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user