-
Notifications
You must be signed in to change notification settings - Fork 0
/
wgoth.go
188 lines (127 loc) · 4.08 KB
/
wgoth.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
package wgoth
import (
"fmt"
"github.com/gorilla/pat"
"github.com/quasoft/memstore"
"github.com/gorilla/sessions"
"github.com/markbates/goth"
"github.com/ralfonso-directnic/wgoth/gothic"
"github.com/markbates/goth/providers/auth0"
"github.com/markbates/goth/providers/digitalocean"
"github.com/markbates/goth/providers/github"
"github.com/markbates/goth/providers/google"
"github.com/markbates/goth/providers/okta"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
)
var provider_name string
var protocol string
var sslcrt string
var sslkey string
var host string
var port string
var Store sessions.Store
func Session(authkey string,enckey string){
Store = memstore.NewMemStore(
[]byte(authkey),
[]byte(enckey),
)
gothic.Store = Store
}
func Init(provider_nm string, host_str string, port_str string, sslkey_str string, sslcrt_str string, args ...string) {
provider_name = provider_nm
sslkey = sslkey_str
sslcrt = sslcrt_str
host = host_str
port = port_str
if len(sslkey) > 0 && len(sslcrt) > 0 {
protocol = "https"
} else {
protocol = "http"
}
if len(host) == 0 {
host, _ = os.Hostname()
}
if len(port) == 0 {
port = "8080"
}
var provider goth.Provider
switch provider_name {
case "auth0": //key, secret,domain
provider = auth0.New(getvar(0, args), getvar(1, args), authurl(provider_name), getvar(2, args))
case "google": //key,secret
provider = google.New(getvar(0, args), getvar(1, args), authurl(provider_name))
case "digitalocean": //key,secret
provider = digitalocean.New(getvar(0, args), getvar(1, args), authurl(provider_name), "read")
case "okta": //id,secret,org_url
provider = okta.New(getvar(0, args), getvar(1, args), getvar(2, args), authurl(provider_name), "openid", "profile", "email")
case "github": //key,secret
provider = github.New(getvar(0, args), getvar(1, args), authurl(provider_name))
default:
provider = google.New(getvar(0, args), getvar(1, args), authurl(provider_name))
}
goth.UseProviders(provider)
}
func authurl(name string) string {
return fmt.Sprintf("%s://%s:%s/auth/%s/callback", protocol, host, port, name)
}
func getvar(key int, val []string) string {
if key >= len(val) {
return ""
}
return val[key]
}
func AuthListen(loginTemplate string, router func(rtr *pat.Router), fn func(user goth.User, res http.ResponseWriter, req *http.Request),logout func(res http.ResponseWriter, req *http.Request) ) {
//could be a path or could be a string
f, err := ioutil.ReadFile(loginTemplate)
if err == nil {
loginTemplate = string(f) // we found a file with path loginTemplate so we set this to the new string, otherwise it's just a string template
}
p := pat.New()
router(p)
p.Get("/auth/{provider}/callback", func(res http.ResponseWriter, req *http.Request) {
user, err := gothic.CompleteUserAuth(res, req)
if err != nil {
fmt.Fprintln(res, err)
return
}
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
fn(user, res, req)
})
p.Get("/logout/{provider}", func(res http.ResponseWriter, req *http.Request) {
gothic.Logout(res, req)
logout(res,req)
res.Header().Set("Location", "/")
res.WriteHeader(http.StatusTemporaryRedirect)
})
p.Get("/status/{provider}", func(res http.ResponseWriter, req *http.Request) {
gothUser,_ := gothic.CompleteUserAuth(res, req)
fn(gothUser, res, req)
})
p.Get("/auth/{provider}", func(res http.ResponseWriter, req *http.Request) {
if gothUser, err := gothic.CompleteUserAuth(res, req); err == nil {
fn(gothUser, res, req)
} else {
log.Println(err)
gothic.BeginAuthHandler(res, req)
}
})
p.Get("/", func(res http.ResponseWriter, req *http.Request) {
t, _ := template.New("tmpl").Parse(loginTemplate)
t.Execute(res, map[string]string{"Provider": provider_name})
})
log.Println("Listening On:", port)
if len(sslkey) > 0 && len(sslcrt) > 0 {
log.Println("OAuth https on")
log.Fatal(http.ListenAndServeTLS(host+":"+port, sslcrt, sslkey, p))
} else {
log.Println("OAuth https off")
log.Fatal(http.ListenAndServe(host+":"+port, p))
}
}