-
Notifications
You must be signed in to change notification settings - Fork 1
/
Authorize.go
63 lines (54 loc) · 1.38 KB
/
Authorize.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
package b2
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
var authURL string = "https://api.backblaze.com/b2api/v1/b2_authorize_account"
type B2 struct {
AccountID string `json:"accountId"`
ApiURL string `json:"apiUrl"`
AuthorizationToken string `json:"authorizationToken"`
DownloadURL string `json:"downloadUrl"`
}
type ErrorJSON struct {
Status int `json:"status"`
Code string `json:"code"`
Message string `json:"message"`
}
func backblaze_error(errMsg ErrorJSON) error {
return fmt.Errorf("%s: %s", errMsg.Code, errMsg.Message)
}
func Authorize(accountID, secretKey string) (*B2, error) {
id_and_key := []byte(accountID + ":" + secretKey)
basic_auth_string := "Basic " + base64.StdEncoding.EncodeToString(id_and_key)
client := &http.Client{}
req, err := http.NewRequest("GET", authURL, nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", basic_auth_string)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
var errMsg ErrorJSON
if err := json.Unmarshal(data, &errMsg); err != nil {
return nil, err
}
return nil, backblaze_error(errMsg)
}
var b2 B2
if err := json.Unmarshal(data, &b2); err != nil {
return nil, err
}
return &b2, nil
}