-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.go
238 lines (211 loc) · 6.06 KB
/
oauth.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
Copyright 2018 Google LLC
Copyright 2016 Kane York
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package intra
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/gorilla/sessions"
"github.com/pkg/errors"
"golang.org/x/oauth2"
)
type SSOHelper struct {
PayloadB64 string
Payload url.Values
Nonce string
ReturnURL string
}
func SSORequest(r *http.Request) (*SSOHelper, error) {
h := &SSOHelper{}
err := r.ParseForm()
if err != nil {
return nil, errors.Wrap(err, "invalid POST form")
}
h.PayloadB64 = r.Form.Get("sso")
enc := base64.URLEncoding
payloadForm, err := enc.DecodeString(strings.TrimSpace(h.PayloadB64))
if err != nil {
return nil, errors.Wrap(err, "invalid b64 encoding")
}
payload, err := url.ParseQuery(string(payloadForm))
if err != nil {
return nil, errors.Wrap(err, "invalid b64 payload")
}
h.Payload = payload
sigBytes, err := hex.DecodeString(r.Form.Get("sig"))
if err != nil {
return nil, errors.Wrap(err, "invalid hex encoding")
}
if !h.IsValid([]byte(r.Form.Get("sso")), sigBytes) {
return nil, errors.Errorf("invalid signature")
}
h.Nonce = payload.Get("nonce")
if h.Nonce == "" {
return nil, errors.Errorf("nonce missing from request")
}
h.ReturnURL = payload.Get("return_sso_url")
return h, nil
}
func (h *SSOHelper) IsValid(payload, sig []byte) bool {
mac := hmac.New(sha256.New, []byte(ssoSecret.Get()))
mac.Write(payload)
expectedSig := mac.Sum(nil)
return hmac.Equal(expectedSig, sig)
}
type secretFile struct {
Filename string
content string
lock sync.Mutex
}
func (f *secretFile) Get() string {
f.lock.Lock()
defer f.lock.Unlock()
if f.content != "" {
return f.content
}
content, err := ioutil.ReadFile(f.Filename)
if err != nil {
fmt.Println("Unable to read SSO secret:", err)
return "XXX"
}
f.content = strings.TrimSpace(string(content))
return f.content
}
var (
ssoSecret = secretFile{
Filename: `/tank/www/keys/discourse_sso_secret`,
}
intraSecret = secretFile{
Filename: `/tank/www/keys/intra_oauth`,
}
cookieSecret = secretFile{
Filename: `/tank/www/keys/oauth_cookies`,
}
)
var cookieStore = sessions.NewCookieStore([]byte(cookieSecret.Get()))
const cookieKey = `intra-oauth`
func HTTPDiscourseSSO(w http.ResponseWriter, r *http.Request) {
sso, err := SSORequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
redirURL := oauthConfig.AuthCodeURL(sso.Nonce, oauth2.SetAuthURLParam("response_type", "code"))
http.Redirect(w, r, redirURL, http.StatusFound)
}
var oauthConfig = oauth2.Config{
ClientID: "00d2a4918d470c47c08448c37fdd170793c2a94320f7971981d461be028f2a35",
ClientSecret: intraSecret.Get(),
Endpoint: oauth2.Endpoint{
AuthURL: "https://api.intra.42.fr/oauth/authorize",
TokenURL: "https://api.intra.42.fr/oauth/token",
},
RedirectURL: "https://home.riking.org/oauth/callback",
Scopes: []string{"public"},
}
type IntraUser struct {
ID int `json:"id"`
Email string `json:"email"`
Login string `json:"login"`
DisplayName string `json:"displayname"`
ImageURL string `json:"image_url"`
IsStaff bool `json:"staff?"`
// Which computer you're on right now
//Location string `json:"location"`
CursusUsers []struct {
Cursus struct {
ID int `json:"id"`
} `json:"cursus"`
} `json:"cursus_users"`
Campus []struct {
ID int `json:"id"`
}
}
const discourseBase = "http://42.riking.org"
func HTTPOauthCallback(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err := r.ParseForm()
if err != nil {
err = errors.Wrap(err, "bad form parameters")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
token, err := oauthConfig.Exchange(ctx, r.Form.Get("code"))
if err != nil {
http.Error(w, errors.Wrap(err, "exchanging token").Error(), http.StatusServiceUnavailable)
return
}
client := oauthConfig.Client(ctx, token)
req, err := http.NewRequest("GET", "https://api.intra.42.fr/v2/me", nil)
if err != nil {
panic(err)
}
resp, err := client.Do(req)
if err != nil {
http.Error(w, errors.Wrap(err, "contacting Intra").Error(), http.StatusServiceUnavailable)
return
}
respBody, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
http.Error(w, errors.Wrap(err, "could not read from Intra").Error(), http.StatusInternalServerError)
return
}
var user IntraUser
err = json.NewDecoder(bytes.NewReader(respBody)).Decode(&user)
if err != nil {
http.Error(w, errors.Wrap(err, "could not read JSON").Error(), http.StatusInternalServerError)
return
}
sso := make(url.Values)
sso.Set("nonce", r.Form.Get("state"))
sso.Set("name", user.DisplayName)
sso.Set("username", user.Login)
sso.Set("email", user.Email)
sso.Set("external_id", strconv.Itoa(user.ID))
sso.Set("avatar_url", fmt.Sprintf("https://cdn.intra.42.fr/users/medium_%s.jpg", user.Login))
if user.IsStaff {
sso.Set("moderator", "true")
} else {
sso.Set("moderator", "false")
}
payload := sso.Encode()
b64Payload := base64.URLEncoding.EncodeToString([]byte(payload))
mac := hmac.New(sha256.New, []byte(ssoSecret.Get()))
mac.Write([]byte(b64Payload))
sig := mac.Sum(nil)
hexSig := hex.EncodeToString(sig)
ssoValues := make(url.Values)
ssoValues.Set("sso", b64Payload)
ssoValues.Set("sig", hexSig)
url, err := url.Parse(fmt.Sprintf("%s/session/sso_login", discourseBase))
if err != nil {
panic(err)
}
url.RawQuery = ssoValues.Encode()
http.Redirect(w, r, url.String(), http.StatusFound)
}