-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccess_token.go
90 lines (77 loc) · 2.27 KB
/
access_token.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
//********************************************************************************************************************//
//
// Copyright (C) 2018 - 2021 J&J Ideenschmiede GmbH <[email protected]>
//
// This file is part of gosw6.
// All code may be used. Feel free and maybe code something better.
//
// Author: Jonas Kwiedor (aka gowizzard)
//
//********************************************************************************************************************//
package gosw6
import (
"encoding/json"
)
// AccessTokenBody is to structure the data
type AccessTokenBody struct {
GrantType string `json:"grant_type"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
Scopes string `json:"scopes"`
Username string `json:"username"`
Password string `json:"password"`
}
// AccessTokenReturn is to decode the json return
type AccessTokenReturn struct {
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
Errors []struct {
Code string `json:"code"`
Status string `json:"status"`
Title string `json:"title"`
Detail interface{} `json:"detail"`
Meta struct {
Trace []struct {
File string `json:"file"`
Line int `json:"line"`
Function string `json:"function"`
Class string `json:"class"`
Type string `json:"type"`
} `json:"trace"`
File string `json:"file"`
Line int `json:"line"`
} `json:"meta"`
} `json:"errors"`
}
// AccessToken is to get a bearer token from the system
func AccessToken(body AccessTokenBody, r Request) (AccessTokenReturn, error) {
// Convert body
convert, err := json.Marshal(body)
if err != nil {
return AccessTokenReturn{}, err
}
// Set config for request
c := Config{
Path: "/api/oauth/token",
Method: "POST",
Body: convert,
AccessToken: true,
}
// Send request
response, err := c.Send(r)
if err != nil {
return AccessTokenReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode AccessTokenReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return AccessTokenReturn{}, err
}
// Return data
return decode, err
}