← history for internal/handler/oidc.go
4e7342aeinternal/handler/oidc.go99 lines⬡ raw↓ download
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package handler

import (
    "crypto/rand"
    "encoding/base64"
    "net/http"
    "net/url"

    "github.com/coreos/go-oidc/v3/oidc"
    "github.com/gin-contrib/sessions"
    "github.com/gin-gonic/gin"
    "golang.org/x/oauth2"
)

func PrintLoginRequestHandler() func(c *gin.Context) {
    return func(c *gin.Context) {
        if sessions.Default(c).Get("user") == nil {
            c.Redirect(http.StatusFound, "/login")
            c.Abort()
        }
    }
}

func RootHandler() func(c *gin.Context) {
    return func(c *gin.Context) {
        if user := sessions.Default(c).Get("user"); user != nil {
            c.String(http.StatusOK, "Logged in as %v. Go to /profile or /logout", user)
            return
        }
        c.String(http.StatusOK, "Not logged in. Go to /login")
    }
}

func LogoutHandler(clientID string, issuer string) func(c *gin.Context) {
    return func(c *gin.Context) {
        s := sessions.Default(c)
        s.Clear()
        s.Save()
        q := url.Values{"client_id": {clientID}}
        c.Redirect(http.StatusFound, issuer+"/protocol/openid-connect/logout?"+q.Encode())
    }
}

func ProfileHandler() func(c *gin.Context) {
    return func(c *gin.Context) {
        s := sessions.Default(c)
        c.JSON(http.StatusOK, gin.H{"user": s.Get("user"), "email": s.Get("email")})
    }
}

func CallbackHandler(oauthCfg *oauth2.Config, verifier *oidc.IDTokenVerifier) func(c *gin.Context) {
    return func(c *gin.Context) {
        s := sessions.Default(c)
        if s.Get("state") != c.Query("state") {
            c.String(http.StatusBadRequest, "invalid state")
            return
        }

        token, err := oauthCfg.Exchange(c.Request.Context(), c.Query("code"))
        if err != nil {
            c.String(http.StatusUnauthorized, "code exchange failed: %v", err)
            return
        }
        rawIDToken, _ := token.Extra("id_token").(string)
        idToken, err := verifier.Verify(c.Request.Context(), rawIDToken)
        if err != nil {
            c.String(http.StatusUnauthorized, "invalid id token: %v", err)
            return
        }

        var claims struct {
            Username string `json:"preferred_username"`
            Email    string `json:"email"`
        }
        if err := idToken.Claims(&claims); err != nil {
            c.String(http.StatusInternalServerError, "bad claims: %v", err)
            return
        }

        s.Delete("state")
        s.Set("user", claims.Username)
        s.Set("email", claims.Email)
        s.Save()
        c.Redirect(http.StatusFound, "/profile")
    }
}

func LoginHandler(oauthCfg *oauth2.Config) func(c *gin.Context) {
    return func(c *gin.Context) {
        b := make([]byte, 16)
        rand.Read(b)
        state := base64.URLEncoding.EncodeToString(b)

        s := sessions.Default(c)
        s.Set("state", state)
        s.Save()
        c.Redirect(http.StatusFound, oauthCfg.AuthCodeURL(state))
    }
}