/
api.go
111 lines (80 loc) · 3.24 KB
/
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
package auth
import (
"net/http"
"strconv"
"github.com/CMSgov/bcda-app/log"
customErrors "github.com/CMSgov/bcda-app/bcda/errors"
)
/*
swagger:route POST /auth/token auth GetAuthToken
Get access token
Verifies Basic authentication credentials, and returns a JWT bearer token that can be presented to the other API endpoints.
Produces:
- application/json
Schemes: https
Security:
basic_auth:
Responses:
200: tokenResponse
400: missingCredentials
401: invalidCredentials
500: serverError
*/
func GetAuthToken(w http.ResponseWriter, r *http.Request) {
clientId, secret, ok := r.BasicAuth()
if !ok {
log.API.Errorf("Error Basic Authentication - HTTPS Status Code: %v", http.StatusBadRequest)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
tokenInfo, err := GetProvider().MakeAccessToken(Credentials{ClientID: clientId, ClientSecret: secret}, r)
if err != nil {
switch err.(type) {
case *customErrors.RequestTimeoutError:
//default retrySeconds: 1 second (may convert to environmental variable later)
retrySeconds := strconv.FormatInt(int64(1), 10)
w.Header().Set("Retry-After", retrySeconds)
log.API.Errorf("Error making access token - %s | HTTPS Status Code: %v", err.Error(), http.StatusServiceUnavailable)
http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
case *customErrors.InternalParsingError:
log.API.Errorf("Error making access token - %s | HTTPS Status Code: %v", err.Error(), http.StatusInternalServerError)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
case *customErrors.SSASErrorUnauthorized:
log.API.Errorf("Error making access token - %s | HTTPS Status Code: %v", err.Error(), http.StatusUnauthorized)
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
case *customErrors.SSASErrorBadRequest:
log.API.Errorf("Error making access token - %s | HTTPS Status Code: %v", err.Error(), http.StatusBadRequest)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
default:
log.API.Errorf("Error making access token - %s | HTTPS Status Code: %v", err.Error(), http.StatusInternalServerError)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
return
}
// https://tools.ietf.org/html/rfc6749#section-5.1
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Pragma", "no-cache")
_, err = w.Write([]byte(tokenInfo))
if err != nil {
log.API.Errorf("Error writing response - %s | HTTPS Status Code: %v", err.Error(), http.StatusInternalServerError)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}
/*
swagger:route GET /auth/welcome auth welcome
Test authentication
If a valid token is presented, show a welcome message.
Produces:
- application/json
Schemes: http, https
Security:
bearer_token:
Responses:
200: welcome
401: invalidCredentials
*/
func Welcome(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"success":"Welcome to the Beneficiary Claims Data API!"}`))
}