This repository has been archived by the owner on Jan 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
authentication.go
60 lines (48 loc) · 1.6 KB
/
authentication.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
// Copyright (c) 2015 Ableton AG, Berlin. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package travis
import (
"fmt"
"net/http"
)
// BuildsService handles communication with the builds
// related methods of the Travis CI API.
type AuthenticationService struct {
client *Client
}
type AccessToken string
type accessTokenResponse struct {
Token AccessToken `json:"access_token"`
}
// UsingGithubToken will generate a Travis CI API authentication
// token and call the UsingTravisToken method with it, leaving your
// client authenticated and ready to use.
func (as *AuthenticationService) UsingGithubToken(githubToken string) (AccessToken, *http.Response, error) {
if githubToken == "" {
return "", nil, fmt.Errorf("unable to authenticate client; empty github token provided")
}
var u string = "/auth/github"
var b map[string]string = map[string]string{"github_token": githubToken}
req, err := as.client.NewRequest("POST", u, b, nil)
if err != nil {
return "", nil, err
}
atr := &accessTokenResponse{}
resp, err := as.client.Do(req, atr)
if err != nil {
return "", nil, err
}
as.UsingTravisToken(string(atr.Token))
return atr.Token, resp, err
}
// UsingTravisToken will format and write provided
// travisToken in the AuthenticationService client's headers.
func (as *AuthenticationService) UsingTravisToken(travisToken string) error {
if travisToken == "" {
return fmt.Errorf("unable to authenticate client; empty travis token provided")
}
as.client.Headers["Authorization"] = "token " + travisToken
return nil
}