This repository was archived by the owner on Feb 13, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathBox.go
More file actions
237 lines (202 loc) · 6.07 KB
/
Copy pathBox.go
File metadata and controls
237 lines (202 loc) · 6.07 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package box
import (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"time"
"github.com/dgrijalva/jwt-go"
)
var (
errConfig = errors.New("No BoxSDK API configuration set (try 'NewConfig' or 'NewConfigFromFile')")
)
// Config is the basic structure for a Box API JWT.
type Config struct {
BoxAppSettings AppSettings `json:"boxAppSettings"`
EnterpriseID string `json:"enterpriseID"`
}
// AppSettings is the structure of the configuration information.
type AppSettings struct {
ClientID string `json:"clientID"`
ClientSecret string `json:"clientSecret"`
AppAuth AppAuth `json:"appAuth"`
}
// AppAuth is the authentication details for the configuration.
type AppAuth struct {
PublicKeyID string `json:"publicKeyID"`
PrivateKey string `json:"privateKey"`
Passphrase string `json:"passphrase"`
}
// AccessTokenObject is a token object returned by a successful request to the Box API.
type AccessTokenObject struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
IssuedTokenType string `json:"issued_token_type,omitempty"`
RefreshToken string `json:"refresh_token"`
RestrictedTo []struct {
Scope string `json:"scope,omitempty"`
Object *FileObject `json:"object,omitempty`
} `json:"restricted_to,omitempty"`
TokenType string `json:"token_type,omitempty"`
}
// httpResponse is a structure for capturing data from not OK http statuses.
type httpResponse struct {
Type string `json:"type"`
Status int `json:"status"`
Code string `json:"code"`
ContextInfo struct {
Conflicts struct {
Type string `json:"type"`
ID string `json:"id"`
FileVersion struct {
Type string `json:"type"`
ID string `json:"id"`
Sha1 string `json:"sha1"`
} `json:"file_version"`
SequenceID string `json:"sequence_id"`
Etag string `json:"etag"`
Sha1 string `json:"sha1"`
Name string `json:"name"`
} `json:"conflicts"`
} `json:"context_info"`
HelpURL string `json:"help_url"`
Message string `json:"message"`
RequestID string `json:"request_id"`
}
// SDK is the structure for establishing the connection to the Box API.
type SDK struct {
access *AccessTokenObject
config *Config
client *http.Client
}
// NewConfig sets the configuration for the SDK to establish it's connection.
func (sdk *SDK) NewConfig(cfg *Config) {
sdk.config = cfg
sdk.client = &http.Client{}
}
// NewConfigFromFile sets the config file to read Box info from.
func (sdk *SDK) NewConfigFromFile(filename string) error {
content, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
return err
}
err = json.Unmarshal(content, &sdk.config)
if err != nil {
log.Fatal(err)
return err
}
sdk.client = &http.Client{}
return nil
}
// request runs an HTTP request to the Box API.
func (sdk *SDK) request(method string, url string, body io.Reader, headers map[string]string) ([]byte, error) {
if sdk.config == nil {
return nil, errConfig
}
request, err := http.NewRequest(method, url, body)
if err != nil {
log.Fatalln(err)
return nil, err
}
// Add all user specified headers to the request header.
if headers != nil {
for k, v := range headers {
request.Header.Set(k, v)
}
}
// Check if we have a valid access token object.
if sdk.access != nil {
request.Header.Add("Authorization", "Bearer "+sdk.access.AccessToken)
}
response, err := sdk.client.Do(request)
if err != nil {
log.Fatalln(err)
return nil, err
}
defer response.Body.Close()
respBytes, err := ioutil.ReadAll(response.Body)
response.Body.Close()
log.Println("URL :", url)
log.Println("Status :", response.Status)
if response.StatusCode >= http.StatusBadRequest {
var status httpResponse
json.Unmarshal(respBytes, &status)
return nil, errors.New(status.Type + " " + strconv.Itoa(status.Status) + " (" + status.Code + ") " + "\"" + status.Message + "\"")
}
return respBytes, nil
}
// RequestAccessToken requests a valid access token from the Box API.
func (sdk *SDK) RequestAccessToken() error {
if sdk.config == nil {
return errConfig
}
// Create a unique 32 character long string.
rBytes := make([]byte, 32)
_, err := rand.Read(rBytes)
if err != nil {
log.Fatalln(err)
return err
}
jti := base64.URLEncoding.EncodeToString(rBytes)
// Build the header. This includes the PublicKey as the ID.
token := jwt.New(jwt.SigningMethodRS512)
token.Header["keyid"] = sdk.config.BoxAppSettings.AppAuth.PublicKeyID
// Construct claims.
claims := token.Claims.(jwt.MapClaims)
claims["iss"] = sdk.config.BoxAppSettings.ClientID
claims["sub"] = sdk.config.EnterpriseID
claims["box_sub_type"] = "enterprise"
claims["aud"] = "https://api.box.com/oauth2/token"
claims["jti"] = jti
claims["exp"] = time.Now().Add(time.Second * 3).Unix()
// Decrypt the PrivateKey using its passphrase.
signedKey, err := jwt.ParseRSAPrivateKeyFromPEMWithPassword(
[]byte(sdk.config.BoxAppSettings.AppAuth.PrivateKey),
sdk.config.BoxAppSettings.AppAuth.Passphrase,
)
if err != nil {
log.Fatalln(err)
return err
}
// Build the assertion from the signedKey and claims.
assertion, err := token.SignedString(signedKey)
if err != nil {
log.Fatalln(err)
return err
}
// Build header
header := map[string]string{"Content-Type": "application/x-www-form-urlencoded"}
// Build the access token request.
payload := url.Values{}
payload.Add("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer")
payload.Add("assertion", assertion)
payload.Add("client_id", sdk.config.BoxAppSettings.ClientID)
payload.Add("client_secret", sdk.config.BoxAppSettings.ClientSecret)
// Post the request to the Box API.
response, err := sdk.request("POST", "https://api.box.com/oauth2/token", bytes.NewBufferString(payload.Encode()), header)
if err != nil {
log.Fatalln(err)
return err
}
// Set the access token.
err = json.Unmarshal(response, &sdk.access)
if err != nil {
log.Fatalln(err)
return err
}
go func() {
for range time.Tick(time.Hour) {
sdk.RequestAccessToken()
}
}()
time.Sleep(time.Nanosecond)
return nil
}