-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth.go
61 lines (55 loc) · 1.33 KB
/
auth.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
package scm
import (
"fmt"
"net/http"
"github.com/paloaltonetworks/scm-go/api"
)
type authResponse struct {
Jwt string `json:"access_token"`
Scope string `json:"scope"`
Type string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Err string `json:"error"`
ErrDesc string `json:"error_description"`
}
func (a authResponse) Failed(code int, body []byte) error {
switch code {
case http.StatusOK:
if a.Jwt == "" {
return api.Response{
StatusCode: code,
Errors: []api.Error{{
Message: "Auth successful, but not JWT found in response.",
Details: string(body),
}},
}
}
return nil
case http.StatusBadRequest, http.StatusUnauthorized:
// Auth API actually returns 400 right now, not 401. Checking for
// both right now ensures that if they change this it doesn't suddenly
// break the SDK.
if a.Err != "" || a.ErrDesc != "" {
return api.Response{
StatusCode: code,
Errors: []api.Error{{
Message: fmt.Sprintf("%s: %s", a.Err, a.ErrDesc),
}},
}
}
return api.Response{
StatusCode: code,
Errors: []api.Error{{
Message: "Unauthorized",
Details: string(body),
}},
}
}
return api.Response{
StatusCode: code,
Errors: []api.Error{{
Message: "Unknown error, enable receive logging for more info",
Details: string(body),
}},
}
}