forked from shuheiktgw/go-travis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
69 lines (54 loc) · 1.91 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
61
62
63
64
65
66
67
68
69
// 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 (
"context"
"fmt"
"net/http"
)
// BuildsService handles communication with the builds
// related methods of the Travis CI API.
type AuthenticationService struct {
client *Client
}
// AccessToken is a token to access Travis CI API
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(ctx context.Context, githubToken string) (AccessToken, *http.Response, error) {
if githubToken == "" {
return "", nil, fmt.Errorf("unable to authenticate client; empty github token provided")
}
b := map[string]string{"github_token": githubToken}
h := map[string]string{"Accept": mediaTypeV2}
req, err := as.client.NewRequest(http.MethodPost, "/auth/github", b, h)
if err != nil {
return "", nil, err
}
// This is the only place you need to access Travis API v2.1
// See https://github.com/travis-ci/travis-ci/issues/9273
// FIXME Use API V3 once compatible API is implemented
req.Header.Del("Travis-API-Version")
atr := &accessTokenResponse{}
resp, err := as.client.Do(ctx, 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
}