-
Notifications
You must be signed in to change notification settings - Fork 33
/
http_handlers.go
82 lines (67 loc) · 2.27 KB
/
http_handlers.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
package main
import (
"errors"
"net/http"
"github.com/TykTechnologies/tyk-identity-broker/constants"
"github.com/TykTechnologies/tyk-identity-broker/providers"
tykerrors "github.com/TykTechnologies/tyk-identity-broker/error"
"github.com/gorilla/mux"
)
// Returns a profile ID
func getId(req *http.Request) (string, error) {
id := mux.Vars(req)["id"]
if id == "" {
id = mux.Vars(req)[":id"]
}
if id == "" {
return id, errors.New("no profile id detected")
}
return id, nil
}
// HandleAuth is the main entry point handler for any profile (i.e. /auth/:profile-id/:provider)
func HandleAuth(w http.ResponseWriter, r *http.Request) {
thisId, idErr := getId(r)
if idErr != nil {
tykerrors.HandleError(constants.HandlerLogTag, "Could not retrieve ID", idErr, 400, w, r)
return
}
thisIdentityProvider, thisProfile, err := providers.GetTapProfile(AuthConfigStore, IdentityKeyStore, thisId, TykAPIHandler)
if err != nil {
return
}
pathParams := mux.Vars(r)
thisIdentityProvider.Handle(w, r, pathParams, thisProfile)
return
}
// HandleAuthCallback Is a callback URL passed to OAuth providers such as Social, handles completing an auth request
func HandleAuthCallback(w http.ResponseWriter, r *http.Request) {
thisId, idErr := getId(r)
if idErr != nil {
tykerrors.HandleError(constants.HandlerLogTag, "Could not retrieve ID", idErr, 400, w, r)
return
}
thisIdentityProvider, thisProfile, err := providers.GetTapProfile(AuthConfigStore, IdentityKeyStore, thisId, TykAPIHandler)
if err != nil {
tykerrors.HandleError(constants.HandlerLogTag, err.Message, err.Error, err.Code, w, r)
return
}
thisIdentityProvider.HandleCallback(w, r, tykerrors.HandleError, thisProfile)
return
}
func HandleHealthCheck(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func HandleMetadata(w http.ResponseWriter, r *http.Request) {
thisId, idErr := getId(r)
if idErr != nil {
tykerrors.HandleError(constants.HandlerLogTag, "Could not retrieve ID", idErr, 400, w, r)
return
}
thisIdentityProvider, _, err := providers.GetTapProfile(AuthConfigStore, IdentityKeyStore, thisId, TykAPIHandler)
if err != nil {
tykerrors.HandleError(constants.HandlerLogTag, err.Message, err.Error, err.Code, w, r)
return
}
thisIdentityProvider.HandleMetadata(w, r)
return
}