This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
104 lines (83 loc) · 2.76 KB
/
main.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
package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"github.com/simonknittel/go-oauth-proxy/config"
)
// Redirects the user to auth endpoint
func authorizeHandler(w http.ResponseWriter, r *http.Request) {
state := randomBase64String(16)
params := url.Values{}
params.Set("client_id", config.Get("CLIENT_ID"))
params.Set("redirect_url", config.Get("REDIRECT_URI"))
params.Set("scope", config.Get("SCOPE"))
params.Set("state", state)
w.Header().Add("Location", fmt.Sprintf("%s?%s",
config.Get("GITHUB_AUTH_ENDPOINT"),
params.Encode()))
w.Header().Add("Set-Cookie", fmt.Sprintf("state=%s; HttpOnly=true", state))
w.WriteHeader(307)
}
// Handles GitHub's callback and redirects user to the frontend with GitHub's
// access token in response body.
func callbackHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
stateCookie, err := r.Cookie("state")
if err != nil {
fmt.Println("Error while reading the state cookie.")
w.Header().Add("Location", fmt.Sprintf("%s?error=invalid_state_cookie", config.Get("FRONTEND_ENDPOINT")))
clearStateCookie(w)
w.WriteHeader(204)
return
}
returnedState := r.Form.Get("state")
if returnedState != stateCookie.Value {
fmt.Println("Error while comparing the returned state with the state cookie.")
w.Header().Add("Location", fmt.Sprintf("%s?error=states_not_matching", config.Get("FRONTEND_ENDPOINT")))
clearStateCookie(w)
w.WriteHeader(204)
return
}
code := r.Form.Get("code")
getAccessToken(code, w)
}
func getAccessToken(code string, w http.ResponseWriter) {
resp, err := http.PostForm(config.Get("GITHUB_TOKEN_ENDPOINT"), url.Values{
"client_id": {config.Get("CLIENT_ID")},
"client_secret": {config.Get("CLIENT_SECRET")},
"code": {code}})
if err != nil {
fmt.Println("Error while while requesting the access token.")
fmt.Printf("%s\n", err)
clearStateCookie(w)
w.WriteHeader(500)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error while reading response body.")
clearStateCookie(w)
w.WriteHeader(500)
return
}
redirectToFrontend(body, w)
}
func redirectToFrontend(body []byte, w http.ResponseWriter) {
base64Body := base64.RawURLEncoding.EncodeToString(body)
w.Header().Add("Location", fmt.Sprintf("%s?token=%s", config.Get("FRONTEND_ENDPOINT"), base64Body)) // TODO: Not cool. Token should propably not be visible in the URL
clearStateCookie(w)
w.WriteHeader(307)
}
func main() {
config.Init()
config.Required()
http.HandleFunc(config.Get("AUTHORIZE_PATH"), authorizeHandler)
http.HandleFunc(config.Get("CALLBACK_PATH"), callbackHandler)
log.Printf("About to listen on port: %s", config.Get("PORT"))
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", config.Get("PORT")), nil))
}