forked from igk1972/netlify-cms-oauth-provider-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (145 loc) · 4.43 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
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
package main
import (
"fmt"
"net/http"
"os"
"github.com/gorilla/pat"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
"github.com/markbates/goth/providers/bitbucket"
"github.com/markbates/goth/providers/github"
"github.com/markbates/goth/providers/gitlab"
)
var (
host = "localhost:3000"
bindAddr = "127.0.0.1:3000"
)
const (
script = `<!DOCTYPE html><html><body>%s<script>
if (!window.opener) {
window.opener = {
postMessage: function(action, origin) {
console.log(action, origin);
}
}
}
(function(status, provider, result) {
function recieveMessage(e) {
console.log("Recieve message:", e);
// send message to main window with da app
window.opener.postMessage(
"authorization:" + provider + ":" + status + ":" + result,
e.origin
);
}
window.addEventListener("message", recieveMessage, false);
// Start handshare with parent
console.log("Sending message:", provider)
window.opener.postMessage(
"authorizing:" + provider,
"*"
);
})("%s", "%s", %s)
</script></body></html>`
)
// GET /
func handleMain(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/html; charset=utf-8")
res.WriteHeader(http.StatusOK)
res.Write([]byte(``))
}
// GET /auth Page redirecting after provider get param
func handleAuth(res http.ResponseWriter, req *http.Request) {
url := fmt.Sprintf("https://%s/auth/%s", host, req.FormValue("provider"))
fmt.Printf("redirect to %s\n", url)
http.Redirect(res, req, url, http.StatusTemporaryRedirect)
}
// GET /auth/provider Initial page redirecting by provider
func handleAuthProvider(res http.ResponseWriter, req *http.Request) {
gothic.BeginAuthHandler(res, req)
}
// GET /callback/{provider} Called by provider after authorization is granted
func handleCallbackProvider(res http.ResponseWriter, req *http.Request) {
var (
status string
result string
)
provider, errProvider := gothic.GetProviderName(req)
user, errAuth := gothic.CompleteUserAuth(res, req)
status = "error"
if errProvider != nil {
fmt.Printf("provider failed with '%s'\n", errProvider)
result = fmt.Sprintf("\"%s\"", errProvider)
} else if errAuth != nil {
fmt.Printf("auth failed with '%s'\n", errAuth)
result = fmt.Sprintf("\"%s\"", errAuth)
} else {
fmt.Printf("Logged in as %s user: %s (%s)\n", user.Provider, user.Email, user.AccessToken)
status = "success"
result = fmt.Sprintf(`JSON.stringify({"token":"%s", "provider":"%s"})`, user.AccessToken, user.Provider)
}
res.Header().Set("Content-Type", "text/html; charset=utf-8")
res.WriteHeader(http.StatusOK)
res.Write([]byte(fmt.Sprintf(script, status, status, provider, result)))
}
// GET /refresh
func handleRefresh(res http.ResponseWriter, req *http.Request) {
fmt.Printf("refresh with '%s'\n", req)
res.Write([]byte(""))
}
// GET /success
func handleSuccess(res http.ResponseWriter, req *http.Request) {
fmt.Printf("success with '%s'\n", req)
res.Write([]byte(""))
}
func init() {
if hostEnv, ok := os.LookupEnv("HOST"); ok {
host = hostEnv
}
if bindEnv, ok := os.LookupEnv("BIND"); ok {
bindAddr = bindEnv
}
var (
gitlabProvider goth.Provider
)
if gitlabServer, ok := os.LookupEnv("GITLAB_SERVER"); ok {
gitlabProvider = gitlab.NewCustomisedURL(
os.Getenv("GITLAB_KEY"), os.Getenv("GITLAB_SECRET"),
fmt.Sprintf("https://%s/callback/gitlab", host),
fmt.Sprintf("https://%s/oauth/authorize", gitlabServer),
fmt.Sprintf("https://%s/oauth/token", gitlabServer),
fmt.Sprintf("https://%s/api/v3/user", gitlabServer),
)
} else {
gitlabProvider = gitlab.New(
os.Getenv("GITLAB_KEY"), os.Getenv("GITLAB_SECRET"),
fmt.Sprintf("https://%s/callback/gitlab", host),
)
}
goth.UseProviders(
github.New(
os.Getenv("GITHUB_KEY"), os.Getenv("GITHUB_SECRET"),
fmt.Sprintf("https://%s/callback/github", host),
"repo",
),
bitbucket.New(
os.Getenv("BITBUCKET_KEY"), os.Getenv("BITBUCKET_SECRET"),
fmt.Sprintf("https://%s/callback//bitbucket", host),
),
gitlabProvider,
)
}
func main() {
router := pat.New()
router.Get("/callback/{provider}", handleCallbackProvider)
router.Get("/auth/{provider}", handleAuthProvider)
router.Get("/auth", handleAuth)
router.Get("/refresh", handleRefresh)
router.Get("/success", handleSuccess)
router.Get("/", handleMain)
//
http.Handle("/", router)
//
fmt.Printf("Started running on %s for %s\n", bindAddr, host)
fmt.Println(http.ListenAndServe(bindAddr, nil))
}