-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoidc.go
185 lines (158 loc) · 5.01 KB
/
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
kubekey is a client-go credentials plugin for kubectl
Copyright (C) 2019 - 2025 Meteorologisk Institutt (MET Norway)
Postboks 43 Blindern, 0313 OSLO, Norway - www.met.no
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//
// The idea here is to spawn up a short lived http server on localhost and spin up the users browser for login through SSO.
// See https://auth0.com/blog/oauth-2-best-practices-for-native-apps/
//
package main
import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
"time"
oidc "github.com/coreos/go-oidc/v3/oidc"
"github.com/pkg/browser"
"golang.org/x/oauth2"
)
type OIDC struct {
ClientID string
ClientSecret string
IDPIssuerURL string
}
func newState() string {
random := make([]byte, 32)
_, err := rand.Read(random)
if err != nil {
log.Fatal(err)
}
return base64.URLEncoding.EncodeToString(random)
}
// Authorization Code Flow with Proof Key for Code Exchange (PKCE)
func pkceCredentials() (oauth2.AuthCodeOption, oauth2.AuthCodeOption, oauth2.AuthCodeOption) {
// URLEncoding provides: A-Z, a-z, 0-9, -=_ ref: https://tools.ietf.org/html/rfc4648#section-5
// PKCE allowed chars : A-Z, a-z, 0-9, -._~ ref: https://www.oauth.com/oauth2-servers/pkce/authorization-request/
// => needs to replace = with .
plainCodeChallenge := strings.ReplaceAll(newState(), "=", ".")
s256 := sha256.New()
s256.Write([]byte(plainCodeChallenge))
codeChallenge := oauth2.SetAuthURLParam("code_challenge", base64.RawURLEncoding.EncodeToString(s256.Sum(nil)))
codeChallengeMethod := oauth2.SetAuthURLParam("code_challenge_method", "S256")
codeVerifier := oauth2.SetAuthURLParam("code_verifier", plainCodeChallenge)
return codeChallenge, codeChallengeMethod, codeVerifier
}
func (cfg *OIDC) Authenticate(tokenChan chan<- string) {
mux := &http.ServeMux{}
srv := &http.Server{
Handler: mux,
}
closeSrv := func(token string, err error) {
tokenChan <- token
if err != nil {
log.Print(err)
}
time.Sleep(2 * time.Second)
srv.Close()
}
// Listen to random port on localhost
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatal(err)
}
baseURL := fmt.Sprintf("http://%s", listener.Addr())
// OIDC Client
ctx := context.Background()
provider, err := oidc.NewProvider(ctx, cfg.IDPIssuerURL)
if err != nil {
log.Fatal(err)
}
oidcConfig := &oidc.Config{
ClientID: cfg.ClientID,
}
verifier := provider.Verifier(oidcConfig)
// Configure oAuth2
config := oauth2.Config{
ClientID: cfg.ClientID,
ClientSecret: cfg.ClientSecret,
Endpoint: provider.Endpoint(),
RedirectURL: baseURL + "/callback",
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
}
state := newState()
codeChallenge, codeChallengeMethod, codeVerifier := pkceCredentials()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, config.AuthCodeURL(state, codeChallenge, codeChallengeMethod), http.StatusFound)
})
mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
var token string
var err error
fail := func(msg string) {
failMsg := failMsg{Msg: msg}
w.WriteHeader(http.StatusInternalServerError)
tmpl, err := parseTmplFiles("html_fail.tmpl")
if err != nil {
log.Fatal(err)
}
tmpl.Execute(w, failMsg)
go closeSrv(token, err)
}
if r.URL.Query().Get("state") != state {
fail("OAuth2 error: state did not match")
return
}
oauth2Token, err := config.Exchange(ctx, r.URL.Query().Get("code"), codeVerifier)
if err != nil {
fail("Failed to exhange token: " + err.Error())
return
}
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
fail("No id_token field in oauth2 token.")
return
}
idToken, err := verifier.Verify(ctx, rawIDToken)
if err != nil {
fail("Failed to verify ID Token: " + err.Error())
return
}
token = fmt.Sprintf("%s.%d", rawIDToken, idToken.Expiry.Unix())
var claims struct {
Name string `json:"name"`
Picture string `json:"picture"`
}
err = idToken.Claims(&claims)
if err != nil {
fail("Obtained IDToken, but some info seems to be missing. (Might still be working.)" + err.Error())
return
}
tmpl, err := parseTmplFiles("html_ok.tmpl")
if err != nil {
log.Fatal(err)
}
tmpl.Execute(w, claims)
go closeSrv(token, nil)
})
browser.Stdout = os.Stderr
browser.OpenURL(baseURL + "/")
srv.Serve(listener)
}