mirror of
https://github.com/janishutz/oidclogin.git
synced 2026-10-08 17:26:20 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6fb820d6af | ||
|
|
0c4a463052 | ||
|
|
d744f68b12 |
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.
|
||||
@@ -112,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 {
|
||||
@@ -125,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 {
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
<html>
|
||||
<head>
|
||||
<!-- NOTE: You should definitely host this yourself! (and you can adjust the style that way, too!) -->
|
||||
<link rel="stylesheet" href="https://cdn.jsdelir.net/gh/janishutz/oidclogin/public/style.css">
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/janishutz/oidclogin/public/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1>401</h1>
|
||||
<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>
|
||||
|
||||
@@ -1,11 +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.jsdelir.net/gh/janishutz/oidclogin/public/style.css">
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/janishutz/oidclogin/public/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1>500</h1>
|
||||
<p>Login Failed!</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>
|
||||
|
||||
+19
-3
@@ -1,4 +1,5 @@
|
||||
html, body {
|
||||
html,
|
||||
body {
|
||||
background-color: #050505;
|
||||
color: #FFFFFF;
|
||||
width: 100vw;
|
||||
@@ -12,16 +13,31 @@ body {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
h1 {
|
||||
.message-wrapper {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.message-wrapper h1 {
|
||||
font-size: 10rem;
|
||||
}
|
||||
|
||||
p {
|
||||
.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;
|
||||
|
||||
Reference in new issue
Block a user