forked from micahhausler/k8s-oidc-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
127 lines (113 loc) · 2.97 KB
/
server.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
package main
import (
"encoding/json"
"log"
"net/http"
"net/url"
"strings"
"gopkg.in/gin-gonic/gin.v1"
)
type Server struct{}
func (s *Server) ServeIndex(context *gin.Context) {
req, err := buildInitalQuery()
if err != nil {
context.AbortWithError(500, err)
return
}
context.Redirect(307, req.URL.String())
}
const client_id = "957307268185-i0rjb14s8r9g8g61okcj422ee8vagv95.apps.googleusercontent.com"
const client_secret = "RrYOyJiCwFkiQKd7--bmdeJJ"
func (s *Server) Auth(c *gin.Context) {
oauthErr := c.DefaultQuery("error", "-1")
if oauthErr != "-1" {
c.String(500, "Could not create K8s user. Error: %s", oauthErr)
return
}
oauthCode := c.DefaultQuery("code", "-1")
if oauthCode == "-1" {
c.String(500, "Invalid OAuth Code.")
return
}
form := url.Values{}
form.Add("code", oauthCode)
form.Add("client_id", client_id)
form.Add("client_secret", client_secret)
form.Add("redirect_uri", "https://auth.k8s.wdc.sl.g2trk.com/auth")
form.Add("grant_type", "authorization_code")
resp, err := http.PostForm("https://www.googleapis.com/oauth2/v4/token", form)
if err != nil {
c.AbortWithError(500, err)
return
}
log.Println(resp.Status)
if err != nil {
c.AbortWithError(500, err)
return
}
defer resp.Body.Close()
respT := GoogleTokenResponse{}
err = json.NewDecoder(resp.Body).Decode(&respT)
if err != nil {
c.AbortWithError(500, err)
return
}
email, err := s.getUserEmail(respT)
if err != nil {
c.AbortWithError(500, err)
return
}
if !strings.Contains(email, "@go2mobi.com") {
c.AbortWithStatus(http.StatusForbidden)
return
}
log.Printf("%+v", respT)
user := generateUser(email, client_id, client_secret, respT.IDToken, respT.RefreshToken)
c.YAML(200, user)
}
type GoogleTokenResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresIn int `json:"expires_in"`
TokenType string `json:"token_type"`
IDToken string `json:"id_token"`
}
type UserInfo struct {
Email string `json:"email"`
}
type KubectlUser struct {
Name string `yaml:"name"`
KubeUserInfo *KubeUserInfo `yaml:"user"`
}
type KubeUserInfo struct {
AuthProvider *AuthProvider `yaml:"auth-provider"`
}
type AuthProvider struct {
APConfig *APConfig `yaml:"config"`
Name string `yaml:"name"`
}
type APConfig struct {
ClientID string `yaml:"client-id"`
ClientSecret string `yaml:"client-secret"`
IdToken string `yaml:"id-token"`
IdpIssuerUrl string `yaml:"idp-issuer-url"`
RefreshToken string `yaml:"refresh-token"`
}
func (s *Server) getUserEmail(gtr GoogleTokenResponse) (string, error) {
uri, _ := url.Parse("https://www.googleapis.com/oauth2/v3/userinfo")
q := uri.Query()
q.Set("alt", "json")
q.Set("access_token", gtr.AccessToken)
uri.RawQuery = q.Encode()
resp, err := http.Get(uri.String())
defer resp.Body.Close()
if err != nil {
return "", err
}
ui := &UserInfo{}
err = json.NewDecoder(resp.Body).Decode(ui)
if err != nil {
return "", err
}
return ui.Email, nil
}