-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauth.go
55 lines (47 loc) · 1.26 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
package govh
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// AuthGetConsumerKey returns the consumer key
func AuthGetConsumerKey(ak, region string) (ck string, link string, err error) {
endpoint := RegionEndpoint(region)
type response struct {
ValidationURL string
ConsumerKey string
State string
}
var jresp response
client := &http.Client{}
body := "{\"accessRules\":[{\"method\":\"GET\",\"path\":\"/*\"},{\"method\":\"POST\",\"path\":\"/*\"},{\"method\":\"DELETE\",\"path\":\"/*\"},{\"method\":\"PUT\",\"path\":\"/*\"},{\"method\":\"DELETE\",\"path\":\"/*\"} ]}"
url := fmt.Sprintf("%s/%s/auth/credential", endpoint, API_VERSION)
req, err := http.NewRequest("POST", url, strings.NewReader(body))
req.Header.Add("User-Agent", "Govh (https://github.com/Toorop/govh)")
req.Header.Add("X-Ovh-Application", ak)
req.Header.Add("Content-type", "application/json")
resp, err := client.Do(req)
if err != nil {
return
}
// Bad HTTP status
if resp.StatusCode > 399 {
err = errors.New(resp.Status)
return
}
r, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(r, &jresp)
if err != nil {
return
}
resp.Body.Close()
ck = jresp.ConsumerKey
link = jresp.ValidationURL
return
}