mirror of
https://github.com/janishutz/oidclogin.git
synced 2026-10-08 17:26:20 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a9245fd20f | ||
|
|
996f4959fb | ||
|
|
6fb820d6af | ||
|
|
0c4a463052 | ||
|
|
d744f68b12 | ||
|
|
133cbfd86c | ||
|
|
da7dd13e2a |
No files matched your search
@@ -8,3 +8,37 @@ OIDC_CLIENT_SECRET=<client secret>
|
||||
OIDC_ISSUE=<issuer url>
|
||||
```
|
||||
If these env vars are not present, then depending on configuration, stubs will be used for the endpoints or the program will crash
|
||||
|
||||
|
||||
## Usage
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-contrib/sessions/memstore"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/janishutz/oidclogin"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
|
||||
// TODO: Better secret
|
||||
store := memstore.NewStore([]byte("secret"))
|
||||
r.Use(sessions.Sessions("jhid", store))
|
||||
|
||||
oidclogin.Configure(r, os.Getenv("APP_BASE_URL"), "/account", false)
|
||||
r.LoadHTMLGlob("public/*")
|
||||
|
||||
r.GET("/account", oidclogin.EnsureLogin(false), func(ctx *gin.Context) {
|
||||
ctx.HTML(200, "main.tmpl", gin.H{})
|
||||
})
|
||||
|
||||
r.Run()
|
||||
}
|
||||
```
|
||||
This example further needs the environment variable `APP_BASE_URL` set to something like `https://app.example.org`.
|
||||
Further, you should create a template file called `main.tmpl` and also copy over the template files in the `public` directory here and edit them.
|
||||
+14
-7
@@ -10,7 +10,7 @@ import (
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
func LoginHandler(c *gin.Context) {
|
||||
func loginHandler(c *gin.Context) {
|
||||
// Set up user session
|
||||
state := 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)))
|
||||
}
|
||||
|
||||
func CallbackHandler(c *gin.Context) {
|
||||
func callbackHandler(c *gin.Context) {
|
||||
session := sessions.Default(c)
|
||||
state := session.Get("jhid_oauth_state")
|
||||
nonce := session.Get("jhid_oauth_nonce")
|
||||
@@ -83,14 +83,20 @@ func CallbackHandler(c *gin.Context) {
|
||||
}
|
||||
if err := idToken.Claims(&claims); err != nil {
|
||||
log.Println("Token claims generation failed", err)
|
||||
c.AbortWithStatus(500)
|
||||
c.HTML(500, "oidcerror.tmpl", gin.H{
|
||||
"error": "ERR_AUTH",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// Verify NONCE
|
||||
if nonce != claims.Nonce {
|
||||
log.Println("Token verificcation failed", err)
|
||||
c.AbortWithStatus(500)
|
||||
c.HTML(500, "oidcerror.tmpl", gin.H{
|
||||
"error": "ERR_AUTH",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -106,6 +112,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 {
|
||||
@@ -119,6 +126,8 @@ 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 {
|
||||
@@ -126,9 +135,7 @@ func EnsureLogin(redirectFail bool) func(c *gin.Context) {
|
||||
c.Abort()
|
||||
return
|
||||
} else {
|
||||
c.HTML(401, "autherror.tmpl", gin.H{
|
||||
"error": "ERR_AUTH",
|
||||
})
|
||||
c.HTML(401, "autherror.tmpl", gin.H{})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ var (
|
||||
defaultRedirect string
|
||||
)
|
||||
|
||||
// Configure and set up the login SDK.
|
||||
func Configure(r *gin.Engine, app_url string, default_redirect string, stubs_on_unconfigured bool) {
|
||||
issuer := os.Getenv("OIDC_ISSUER")
|
||||
clientID := os.Getenv("OIDC_CLIENT_ID")
|
||||
@@ -58,8 +59,23 @@ func Configure(r *gin.Engine, app_url string, default_redirect string, stubs_on_
|
||||
Scopes: []string{oidc.ScopeOpenID, "email", "profile"},
|
||||
}
|
||||
|
||||
r.GET("/auth/v2/login", LoginHandler)
|
||||
r.GET("/auth/v2/verify", CallbackHandler)
|
||||
r.GET("/auth/v2/login", loginHandler)
|
||||
r.GET("/auth/v2/verify", callbackHandler)
|
||||
r.GET("/auth/v2/check", EnsureLogin(false), func(ctx *gin.Context) { ctx.JSON(200, gin.H{"success": "true"}) })
|
||||
|
||||
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",
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<html>
|
||||
<head>
|
||||
<!-- NOTE: You should definitely host this yourself! (and you can adjust the style that way, too!) -->
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/janishutz/oidclogin/public/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="message-wrapper">
|
||||
<h1>401</h1>
|
||||
<p>Unauthorized</p>
|
||||
</div>
|
||||
<p>You are not authorized to view this page. Please log in</p>
|
||||
<a href="/">Back Home</a>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,9 +1,15 @@
|
||||
<html>
|
||||
<head>
|
||||
|
||||
<!-- NOTE: You should definitely host this yourself! (and you can adjust the style that way, too!) -->
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/janishutz/oidclogin/public/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Login Failed!</h1>
|
||||
<p>{{ .error }}</p>
|
||||
<div class="message-wrapper">
|
||||
<h1>500</h1>
|
||||
<p>Internal Server Error</p>
|
||||
</div>
|
||||
<p>There was an error logging you in. Please try again. We are sorry for the inconvenience</p>
|
||||
<a href="/">Back Home</a>
|
||||
<p class="error-msg">{{ .error }}</p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,49 @@
|
||||
html,
|
||||
body {
|
||||
background-color: #050505;
|
||||
color: #FFFFFF;
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: sans-serif;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.message-wrapper {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.message-wrapper h1 {
|
||||
font-size: 10rem;
|
||||
}
|
||||
|
||||
.message-wrapper p {
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
|
||||
a {
|
||||
color: white;
|
||||
background-color: #101066;
|
||||
padding: 10px;
|
||||
border-radius: 10px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.error-msg {
|
||||
font-size: 0.8rem;
|
||||
position: fixed;
|
||||
bottom: 10px;
|
||||
left: 0;
|
||||
width: 100vw;
|
||||
text-align: center;
|
||||
color: #888888;
|
||||
}
|
||||
@@ -1,22 +1,26 @@
|
||||
package oidclogin
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"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"}) })
|
||||
}
|
||||
|
||||
func stubsHandler(c *gin.Context) {
|
||||
redir := c.Query("returnTo")
|
||||
session := sessions.Default(c)
|
||||
userFunc("stubs", "Stubs User", "example@example.com")
|
||||
session.Set("jhid_auth", true)
|
||||
session.Set("jhid_uid", "stubs")
|
||||
session.Save()
|
||||
if redir != "" {
|
||||
c.Redirect(307, redir)
|
||||
} else {
|
||||
c.Redirect(307, defaultRedirect)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user