-
Notifications
You must be signed in to change notification settings - Fork 7
/
authentication-api.go
141 lines (113 loc) · 3.63 KB
/
authentication-api.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
package rest
import (
"github.com/apibrew/apibrew/pkg/errors"
"github.com/apibrew/apibrew/pkg/model"
"github.com/apibrew/apibrew/pkg/service"
"github.com/gorilla/mux"
"net/http"
)
type AuthenticationApi interface {
ConfigureRouter(r *mux.Router)
}
type authenticationApi struct {
service service.AuthenticationService
}
func (m authenticationApi) ConfigureRouter(router *mux.Router) {
subRoute := router.PathPrefix("/authentication/token").Subrouter()
// collection level operations
subRoute.HandleFunc("", m.handleAuthentication).Methods("POST")
subRoute.HandleFunc("/oauth2", m.handleOauth2).Methods("POST")
subRoute.HandleFunc("", m.handleRefreshToken).Methods("PUT")
subRoute.HandleFunc("", m.handleGetToken).Methods("GET")
}
func (m authenticationApi) handleOauth2(writer http.ResponseWriter, request *http.Request) {
if err := request.ParseForm(); err != nil {
handleError(writer, err)
return
}
var params = request.Form
var clientId = params.Get("client_id")
var clientSecret = params.Get("client_secret")
var grantType = params.Get("grant_type")
//var code = params.Get("code")
//var redirectUri = params.Get("redirect_uri")
//var refreshToken = params.Get("refresh_token")
//var scope = params.Get("scope")
var term = params.Get("term")
var minimizeToken = params.Get("minimize") == "true"
if term == "" {
term = "MIDDLE"
}
switch grantType {
case "client_credentials":
{
token, serr := m.service.Authenticate(request.Context(), clientId, clientSecret, model.TokenTerm(model.TokenTerm_value[term]), minimizeToken)
var resp = &AuthenticationResponse{}
if token != nil {
resp.Token = Token{
Term: token.Term.String(),
Content: token.Content,
Expiration: token.Expiration.AsTime(),
}
}
ServiceResponder().
Writer(writer).
Respond(map[string]interface{}{
"access_token": token.Content,
}, serr)
}
default:
ServiceResponder().
Writer(writer).
Respond(nil, errors.RecordValidationError.WithMessage("unsupported grant type: "+grantType))
}
}
func (m authenticationApi) handleAuthentication(writer http.ResponseWriter, request *http.Request) {
authReq := new(AuthenticationRequest)
err := parseRequestMessage(request, authReq)
if err != nil {
handleError(writer, err)
return
}
token, serr := m.service.Authenticate(request.Context(), authReq.Username, authReq.Password, model.TokenTerm(model.TokenTerm_value[authReq.Term]), authReq.MinimizeToken)
var resp = &AuthenticationResponse{}
if token != nil {
resp.Token = Token{
Term: token.Term.String(),
Content: token.Content,
Expiration: token.Expiration.AsTime(),
}
}
ServiceResponder().
Writer(writer).
Respond(resp, serr)
}
func (m authenticationApi) handleRefreshToken(writer http.ResponseWriter, request *http.Request) {
authReq := new(RefreshTokenRequest)
err := parseRequestMessage(request, authReq)
if err != nil {
handleError(writer, err)
return
}
token, serr := m.service.RenewToken(request.Context(), authReq.Token, model.TokenTerm(model.TokenTerm_value[authReq.Term]))
var resp = &RefreshTokenResponse{}
if token != nil {
resp.Token = Token{
Term: token.Term.String(),
Content: token.Content,
Expiration: token.Expiration.AsTime(),
}
}
ServiceResponder().
Writer(writer).
Respond(resp, serr)
}
func (m authenticationApi) handleGetToken(writer http.ResponseWriter, request *http.Request) {
token, serr := m.service.GetToken(request.Context())
ServiceResponder().
Writer(writer).
Respond(token, serr)
}
func NewAuthenticationApi(service service.AuthenticationService) AuthenticationApi {
return &authenticationApi{service: service}
}