forked from TykTechnologies/tyk-oauth-flow-example
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
105 lines (88 loc) · 2.62 KB
/
Copy pathmain.go
File metadata and controls
105 lines (88 loc) · 2.62 KB
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
package main
import (
log "github.com/Sirupsen/logrus"
"github.com/joho/godotenv"
"html/template"
"net/http"
"os"
)
var (
APIlistenPath string = ""
OrgID string = ""
PolicyID string = ""
BaseAPIID string = ""
GatewayURL string = ""
AdminSecret string = ""
ClientID string = ""
RedirectURI string = ""
)
func index(w http.ResponseWriter, r *http.Request) {
/*if r.Method != "POST" {
http.ServeFile(w, r, "tmpl/index.html")
}*/
tmplVal := make(map[string]string)
tmplVal["APIlistenPath"] = APIlistenPath
tmplVal["OrgID"] = OrgID
tmplVal["PolicyID"] = PolicyID
tmplVal["BaseAPIID"] = BaseAPIID
tmplVal["GatewayURL"] = GatewayURL
tmplVal["AdminSecret"] = AdminSecret
tmplVal["ClientID"] = ClientID
tmplVal["ResponseType"] = "code"
tmplVal["RedirectURI"] = RedirectURI
t, _ := template.ParseFiles("tmpl/index.html")
t.Execute(w, tmplVal)
}
func loginHandler(w http.ResponseWriter, r *http.Request) {
tmplVal := make(map[string]string)
tmplVal["ClientID"] = r.FormValue("client_id")
tmplVal["ResponseType"] = r.FormValue("response_type")
tmplVal["RedirectURI"] = r.FormValue("redirect_uri")
t, _ := template.ParseFiles("tmpl/login.html")
t.Execute(w, tmplVal)
}
func approvedHandler(w http.ResponseWriter, r *http.Request) {
var redirect_uri = r.FormValue("redirect_uri")
var responseType = r.FormValue("response_type")
var clientId = r.FormValue("client_id")
thisResponse, rErr := RequestOAuthToken(APIlistenPath,
redirect_uri, responseType, clientId, "", OrgID, PolicyID, BaseAPIID)
if rErr != nil {
log.Error(rErr)
http.Error(w, "Error!", 500)
}
http.Redirect(w, r, thisResponse.RedirectTo, 301)
}
func finalHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "tmpl/final.html")
}
// init is invoked before main()
func init() {
// loads values from .env into the system
if err := godotenv.Load(".env"); err != nil {
log.Print("No .env file found")
}
APIlistenPath = getEnv("API_LISTENPATH", "oauth2")
OrgID = getEnv("ORG_ID", "")
PolicyID = getEnv("POLICY_ID", "")
BaseAPIID = getEnv("API_ID", "")
GatewayURL = getEnv("GATEWAY_URL", "")
AdminSecret = getEnv("ADMIN_SECRET", "")
ClientID = getEnv("CLIENT_ID", "")
RedirectURI = getEnv("REDIRECT_URI", "")
}
func getEnv(key string, defaultVal string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultVal
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/login", loginHandler)
mux.HandleFunc("/approved", approvedHandler)
mux.HandleFunc("/final", finalHandler)
mux.HandleFunc("/", index)
log.Info("Listening on :8000")
http.ListenAndServe(":8000", mux)
}