-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgin_oidc.go
145 lines (129 loc) · 4.91 KB
/
gin_oidc.go
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package gauth
import (
"context"
"crypto/tls"
"errors"
"net/http"
"net/url"
"github.com/COSSAS/gauth/cookies"
"github.com/COSSAS/gauth/utils"
"github.com/COSSAS/gauth/api"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/gin-gonic/gin"
"golang.org/x/oauth2"
)
func (auth *Authenticator) OIDCRedirectToLogin(ginContext *gin.Context) {
state, err := utils.RandString(16)
if err != nil {
api.JSONErrorStatus(ginContext, http.StatusInternalServerError, errors.New("failed to generate state"))
return
}
nonce, err := utils.RandString(16)
if err != nil {
api.JSONErrorStatus(ginContext, http.StatusInternalServerError, errors.New("failed to generate nonce"))
}
nonceCookie, _ := cookies.NewCookie(cookies.Nonce, nonce)
err = auth.Cookiejar.Store(ginContext, nonceCookie)
if err != nil {
api.JSONErrorStatus(ginContext, http.StatusInternalServerError, errors.New("failed to set nonce"))
return
}
stateCookie, _ := cookies.NewCookie(cookies.State, state)
err = auth.Cookiejar.Store(ginContext, stateCookie)
if err != nil {
api.JSONErrorStatus(ginContext, http.StatusInternalServerError, errors.New("failed to set state"))
return
}
ginContext.Redirect(http.StatusFound, auth.OauthConfig.AuthCodeURL(state, oidc.Nonce(nonce)))
}
func (auth *Authenticator) OIDCCallBack(ginContext *gin.Context, redirectPath string) {
stateCookie, isNew, err := auth.Cookiejar.Get(ginContext, cookies.State)
if isNew || stateCookie == "" || err != nil {
api.JSONErrorStatus(ginContext, http.StatusBadRequest, errors.New("state missing"))
return
}
if stateCookie != ginContext.Query("state") {
api.JSONErrorStatus(ginContext, http.StatusBadRequest, errors.New("state mismatch"))
return
}
localContext := ginContext.Request.Context()
if auth.skipTLSValidation {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
localContext = context.WithValue(localContext, oauth2.HTTPClient, client)
}
oauth2Token, err := auth.OauthConfig.Exchange(localContext, ginContext.Query("code"))
if err != nil {
api.JSONErrorStatus(ginContext, http.StatusUnauthorized, errors.New("could not exchange code for token"))
return
}
rawIDtoken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
api.JSONErrorStatus(ginContext, http.StatusUnauthorized, errors.New("could not parse id_token"))
return
}
verifier := auth.GetTokenVerifier()
verifiedIDToken, err := verifier.Verify(localContext, rawIDtoken)
if err != nil {
api.JSONErrorStatus(ginContext, http.StatusUnauthorized, errors.New("failed to verify ID token"))
return
}
nonce, isNewNonce, err := auth.Cookiejar.Get(ginContext, cookies.Nonce)
if isNewNonce || nonce == "" || err != nil {
api.JSONErrorStatus(ginContext, http.StatusBadRequest, errors.New("invalid or missing nonce"))
return
}
if verifiedIDToken.Nonce != nonce {
api.JSONErrorStatus(ginContext, http.StatusUnauthorized, errors.New("nonce for verified id token did not match"))
return
}
_ = auth.Cookiejar.Delete(ginContext, cookies.Nonce)
accessToken := oauth2Token.AccessToken
userInfo, err := auth.GetProvider().UserInfo(localContext, oauth2.StaticTokenSource(oauth2Token))
if err != nil {
api.JSONErrorStatus(ginContext, http.StatusUnauthorized, errors.New("failed to get user info of access token"))
return
}
if userInfo.Subject != verifiedIDToken.Subject {
// authentik does not support at_hash so we can use the verifacess token.
api.JSONErrorStatus(ginContext, http.StatusUnauthorized, errors.New("user info subject does not match ID token subject"))
return
}
tokenCookie, err := cookies.NewCookie(cookies.Token, accessToken)
if err != nil {
api.JSONErrorStatus(ginContext, http.StatusInternalServerError, errors.New("failed to set access cookie token"))
return
}
err = auth.Cookiejar.Store(ginContext, tokenCookie)
if err != nil {
api.JSONErrorStatus(ginContext, http.StatusInternalServerError, errors.New("internal error could not set access cookie"))
}
_ = auth.Cookiejar.Delete(ginContext, cookies.Nonce)
_ = auth.Cookiejar.Delete(ginContext, cookies.State)
ginContext.Redirect(http.StatusFound, redirectPath)
}
func (auth *Authenticator) Logout(ginContext *gin.Context, redirectPath string) {
_ = auth.Cookiejar.Delete(ginContext, cookies.Token)
var providerJSON map[string]interface{}
if err := auth.GetProvider().Claims(&providerJSON); err != nil {
ginContext.Redirect(http.StatusFound, redirectPath)
return
}
endSessionEndpoint, ok := providerJSON["end_session_endpoint"].(string)
if !ok {
ginContext.Redirect(http.StatusFound, redirectPath)
return
}
logoutURL, err := url.Parse(endSessionEndpoint)
if err != nil {
ginContext.Redirect(http.StatusFound, redirectPath)
return
}
query := logoutURL.Query()
query.Set("client_id", auth.OIDCconfig.ClientID)
query.Set("post_logout_redirect_uri", redirectPath)
logoutURL.RawQuery = query.Encode()
ginContext.Redirect(http.StatusFound, logoutURL.String())
}