// NewOAuthAuthenticator returns a authenticator that uses a oauth refresh token to create access // tokens. // The refresh token can be found in the CM dashboard under Settings > Account Settings > API Credentials. // Specifying the accountID is only required if the API requires the "X-Account" header to be set // (this is true of CM 1.6 at the moment). func NewOAuthAuthenticator(token string, accountID int) Authenticator { return &oAuthSigner{ refreshToken: token, refreshAt: time.Now().Add(-2 * time.Minute), client: httpclient.New(), accountID: accountID, } }
// New returns a API client that uses the given authenticator. // host may be blank in which case client attempts to resolve it using auth. func New(host string, auth Authenticator) *Api { client := httpclient.New() if strings.HasPrefix(host, "http://") { host = host[7:] } else if strings.HasPrefix(host, "https://") { host = host[8:] } a := &Api{ Auth: auth, Host: host, Client: client, } if auth != nil { auth.SetHost(host) } return a }
// NewRL10 returns a API client that uses the information stored in /var/run/rightlink/secret to do // auth and configure the host. The client behaves identically to the client returned by New in // all other regards. func NewRL10() (*Api, error) { client := httpclient.New() rllConfig, err := os.Open(RllSecret) if err != nil { return nil, fmt.Errorf("Failed to load RLL config: %s", err) } defer rllConfig.Close() var port string var secret string scanner := bufio.NewScanner(rllConfig) for scanner.Scan() { line := scanner.Text() elems := strings.Split(line, "=") if len(elems) != 2 { return nil, fmt.Errorf("Invalid RLL configuration line '%s'", line) } switch elems[0] { case "RS_RLL_PORT": port = elems[1] if _, err := strconv.Atoi(elems[1]); err != nil { return nil, fmt.Errorf("Invalid port value '%s'", port) } case "RS_RLL_SECRET": secret = elems[1] } } if err := scanner.Err(); err != nil { return nil, fmt.Errorf("Failed to load RLL config: %s", err) } host := "localhost:" + port auth := NewRL10Authenticator(secret) auth.SetHost(host) api := &Api{ Auth: auth, Host: host, Client: client, } httpclient.Insecure = true return api, nil }
// CanAuthenticate makes a test request to CM 1.5 and returns nil if it is successful. func (a *rl10Authenticator) CanAuthenticate(host string) error { client := httpclient.New() return testAuth(a, client, host, true) }
// CanAuthenticate makes a test request to CM 1.5 and returns nil if it is successful. func (t *tokenAuthenticator) CanAuthenticate(host string) error { client := httpclient.New() return testAuth(t, client, host, false) }
if useHidden { resp, err = client.DoHidden(req) } else { resp, err = client.Do(req) } Ω(err).ShouldNot(HaveOccurred()) }) AfterEach(func() { server.Close() }) Context("created with New", func() { BeforeEach(func() { client = httpclient.New() }) It("follows redirects", func() { Ω(resp.StatusCode).Should(Equal(200)) rb, err := ioutil.ReadAll(resp.Body) Ω(err).ShouldNot(HaveOccurred()) Ω(rb).Should(Equal([]byte("OK"))) }) It("does not dump by default", func() { Ω(stderr.String()).Should(BeEmpty()) }) Context("setting DumpFormat to debug", func() { BeforeEach(func() {