mirror of
https://github.com/janishutz/oidclogin.git
synced 2026-10-08 17:26:20 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f66933a8ce
|
||
|
|
50cfdb0843 | ||
|
|
338693388b | ||
|
|
4153ae83a4 | ||
|
|
374fda716b | ||
|
|
e44d4511f2 | ||
|
|
90d7076726 | ||
|
|
58855ed3a2 | ||
|
|
bb548cc2c3 | ||
|
|
d6a551d80f | ||
|
|
6bcaff2d88 | ||
|
|
e6b8422a1e | ||
|
|
6cb582a762 | ||
|
|
e241d90f42 | ||
|
|
59f635f01e | ||
|
|
d9ca3abb6c | ||
|
|
bc4abcf5e3 | ||
|
|
92182271b0 | ||
|
|
fa9cc4fbcb | ||
|
|
2ae92f8191 | ||
|
|
a9245fd20f | ||
|
|
996f4959fb |
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
module github.com/janishutz/oidclogin
|
module github.com/janishutz/oidclogin
|
||||||
|
|
||||||
go 1.27.1
|
go 1.26.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/coreos/go-oidc/v3 v3.21.0
|
github.com/coreos/go-oidc/v3 v3.21.0
|
||||||
|
|||||||
+25
-2
@@ -35,7 +35,6 @@ func callbackHandler(c *gin.Context) {
|
|||||||
|
|
||||||
if c.Query("state") != state || codeVerifier == nil {
|
if c.Query("state") != state || codeVerifier == nil {
|
||||||
log.Println("State invalid or verifier was not stored")
|
log.Println("State invalid or verifier was not stored")
|
||||||
// TODO: Proper pages
|
|
||||||
c.HTML(500, "oidcerror.tmpl", gin.H{
|
c.HTML(500, "oidcerror.tmpl", gin.H{
|
||||||
"error": "ERR_INVALID_STATE",
|
"error": "ERR_INVALID_STATE",
|
||||||
})
|
})
|
||||||
@@ -92,7 +91,7 @@ func callbackHandler(c *gin.Context) {
|
|||||||
|
|
||||||
// Verify NONCE
|
// Verify NONCE
|
||||||
if nonce != claims.Nonce {
|
if nonce != claims.Nonce {
|
||||||
log.Println("Token verificcation failed", err)
|
log.Println("Token verification failed (nonce missing)")
|
||||||
c.HTML(500, "oidcerror.tmpl", gin.H{
|
c.HTML(500, "oidcerror.tmpl", gin.H{
|
||||||
"error": "ERR_AUTH",
|
"error": "ERR_AUTH",
|
||||||
})
|
})
|
||||||
@@ -113,6 +112,13 @@ func callbackHandler(c *gin.Context) {
|
|||||||
|
|
||||||
session.Set("jhid_auth", true)
|
session.Set("jhid_auth", true)
|
||||||
session.Set("jhid_uid", claims.Uid)
|
session.Set("jhid_uid", claims.Uid)
|
||||||
|
session.Options(sessions.Options{
|
||||||
|
Path: "/",
|
||||||
|
SameSite: sameSiteMode,
|
||||||
|
HttpOnly: true,
|
||||||
|
Secure: prod,
|
||||||
|
MaxAge: 172800, // Expires in 2 days
|
||||||
|
})
|
||||||
session.Save()
|
session.Save()
|
||||||
|
|
||||||
if redir != nil {
|
if redir != nil {
|
||||||
@@ -122,6 +128,23 @@ 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) {
|
func EnsureLogin(redirectFail bool) func(c *gin.Context) {
|
||||||
return (func(c *gin.Context) {
|
return (func(c *gin.Context) {
|
||||||
session := sessions.Default(c)
|
session := sessions.Default(c)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/coreos/go-oidc/v3/oidc"
|
"github.com/coreos/go-oidc/v3/oidc"
|
||||||
@@ -20,37 +21,68 @@ func createRandomString(n int) (string, error) {
|
|||||||
return base64.URLEncoding.EncodeToString(s), nil
|
return base64.URLEncoding.EncodeToString(s), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UserFunc func(userid string, name string, email string)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
config oauth2.Config
|
config oauth2.Config
|
||||||
userFunc func(userid string, name string, email string)
|
userFunc UserFunc
|
||||||
verifier oidc.IDTokenVerifier
|
verifier oidc.IDTokenVerifier
|
||||||
defaultRedirect string
|
defaultRedirect string
|
||||||
|
prod bool
|
||||||
|
sameSiteMode http.SameSite
|
||||||
)
|
)
|
||||||
|
|
||||||
// Configure and set up the login SDK.
|
// Wraps the normal configure function, but also gives you access to change the User Function, which is called upon login.
|
||||||
func Configure(r *gin.Engine, app_url string, default_redirect string, stubs_on_unconfigured bool) {
|
// 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),
|
||||||
|
production bool,
|
||||||
|
) {
|
||||||
|
userFunc = user_function
|
||||||
|
Configure(r, app_url, default_redirect, stubs_on_unconfigured, check_middleware, production)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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), production bool) {
|
||||||
issuer := os.Getenv("OIDC_ISSUER")
|
issuer := os.Getenv("OIDC_ISSUER")
|
||||||
clientID := os.Getenv("OIDC_CLIENT_ID")
|
clientID := os.Getenv("OIDC_CLIENT_ID")
|
||||||
clientSecret := os.Getenv("OIDC_CLIENT_SECRET")
|
clientSecret := os.Getenv("OIDC_CLIENT_SECRET")
|
||||||
|
defaultRedirect = default_redirect
|
||||||
|
prod = production
|
||||||
|
if prod {
|
||||||
|
sameSiteMode = http.SameSiteNoneMode
|
||||||
|
} else {
|
||||||
|
sameSiteMode = http.SameSiteDefaultMode
|
||||||
|
}
|
||||||
|
|
||||||
if issuer == "" || clientID == "" || clientSecret == "" {
|
if issuer == "" || clientID == "" || clientSecret == "" {
|
||||||
if stubs_on_unconfigured {
|
if stubs_on_unconfigured {
|
||||||
log.Println("[JHID] WARNING: OIDC not set up due to missing environment variables. Falling back to stubs")
|
log.Println("[JHID] WARNING: OIDC not set up due to missing environment variables. Falling back to stubs")
|
||||||
startStubs(r)
|
startStubs(r, check_middleware)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
log.Fatal("[JHID] One or more requried environment variables are missing. See docs for more information")
|
log.Fatal("[JHID] One or more requried environment variables are missing. See docs for more information")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
provider, err := oidc.NewProvider(context.Background(), issuer)
|
if userFunc == nil {
|
||||||
verifier = *provider.Verifier(&oidc.Config{ClientID: clientID})
|
log.Println("[JHID] WARNING: No user function defined")
|
||||||
defaultRedirect = default_redirect
|
}
|
||||||
|
|
||||||
|
provider, err := oidc.NewProvider(context.Background(), issuer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("[JHID] Provider resolution failed with error", err)
|
log.Fatal("[JHID] Provider resolution failed with error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
verifier = *provider.Verifier(&oidc.Config{ClientID: clientID})
|
||||||
|
|
||||||
config = oauth2.Config{
|
config = oauth2.Config{
|
||||||
ClientID: clientID,
|
ClientID: clientID,
|
||||||
ClientSecret: clientSecret,
|
ClientSecret: clientSecret,
|
||||||
@@ -61,10 +93,19 @@ func Configure(r *gin.Engine, app_url string, default_redirect string, stubs_on_
|
|||||||
|
|
||||||
r.GET("/auth/v2/login", loginHandler)
|
r.GET("/auth/v2/login", loginHandler)
|
||||||
r.GET("/auth/v2/verify", callbackHandler)
|
r.GET("/auth/v2/verify", callbackHandler)
|
||||||
|
if check_middleware == nil {
|
||||||
|
r.GET("/auth/v2/check", EnsureLogin(false), check_finalizer)
|
||||||
|
} else {
|
||||||
|
log.Println("[JHID] Custom check middleware enabled")
|
||||||
|
r.GET("/auth/v2/check", EnsureLogin(false), check_middleware, check_finalizer)
|
||||||
|
}
|
||||||
|
r.GET("/auth/v2/logout", logoutHandler)
|
||||||
|
|
||||||
log.Println("[JHID] Configured successfully")
|
log.Println("[JHID] Configured successfully")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func check_finalizer(ctx *gin.Context) { ctx.JSON(200, gin.H{"success": "true"}) }
|
||||||
|
|
||||||
func ErrorHandler() gin.HandlerFunc {
|
func ErrorHandler() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<!-- NOTE: You should definitely host this yourself! (and you can adjust the style that way, too!) -->
|
<!-- NOTE: You should definitely host this yourself! (and you can adjust the style that way, too!) -->
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<!-- NOTE: You should definitely host this yourself! (and you can adjust the style that way, too!) -->
|
<!-- NOTE: You should definitely host this yourself! (and you can adjust the style that way, too!) -->
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ a {
|
|||||||
padding: 10px;
|
padding: 10px;
|
||||||
border-radius: 10px;
|
border-radius: 10px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
text-decoration: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.error-msg {
|
.error-msg {
|
||||||
|
|||||||
@@ -1,22 +1,40 @@
|
|||||||
package oidclogin
|
package oidclogin
|
||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
func startStubs(r *gin.Engine) {
|
"github.com/gin-contrib/sessions"
|
||||||
r.GET("/auth/v2/login", func(ctx *gin.Context) {
|
"github.com/gin-gonic/gin"
|
||||||
redir := ctx.Query("returnTo")
|
)
|
||||||
if redir != "" {
|
|
||||||
ctx.Redirect(307, redir)
|
func startStubs(r *gin.Engine, check_middleware func(c *gin.Context)) {
|
||||||
|
r.GET("/auth/v2/login", stubsHandler)
|
||||||
|
r.GET("/auth/v2/verify", stubsHandler)
|
||||||
|
if check_middleware == nil {
|
||||||
|
r.GET("/auth/v2/check", EnsureLogin(false), check_finalizer)
|
||||||
} else {
|
} else {
|
||||||
ctx.Redirect(307, defaultRedirect)
|
log.Println("[JHID] Custom check middleware enabled")
|
||||||
|
r.GET("/auth/v2/check", EnsureLogin(false), check_middleware, check_finalizer)
|
||||||
}
|
}
|
||||||
})
|
r.GET("/auth/v2/logout", logoutHandler)
|
||||||
r.GET("/auth/v2/verify", func(ctx *gin.Context) {
|
}
|
||||||
redir := ctx.Query("returnTo")
|
|
||||||
if redir != "" {
|
func stubsHandler(c *gin.Context) {
|
||||||
ctx.Redirect(307, redir)
|
redir := c.Query("returnTo")
|
||||||
|
session := sessions.Default(c)
|
||||||
|
if userFunc != nil {
|
||||||
|
userFunc("stubs", "Stubs User", "example@example.com")
|
||||||
} else {
|
} else {
|
||||||
ctx.Redirect(307, defaultRedirect)
|
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