Sha256: 47c9a1e339e49dc946722a25884e303d63ab1d18b2c23fff951f9559ec5ee1aa

Contents?: true

Size: 1.05 KB

Versions: 16

Compression:

Stored size: 1.05 KB

Contents

package <%= package %>

import (
	"fmt"
	"net/http"
)

type OAuth2Auth struct {
	// The authentication token
	AccessToken string
}

func (auth *OAuth2Auth) Authenticate(request *http.Request) {
	request.Header.Set("<%= auth.http_header %>", auth.AccessToken)
}

// SetExistingTokenAuth will use a previously acquired access token
func (client *Client) SetExistingOAuth2Auth(accessToken string) {
	client.Authentication = &OAuth2Auth{
		AccessToken: accessToken,
	}
}

// RevokeAuthToken will revoke the access token and remove authentication
// from the client
func (client *Client) RevokeAccessToken() error {
	httpClient := &http.Client{}

	req, err := http.NewRequest("POST", "<%= auth.revoke_url %>", nil)

	if err != nil {
		return err
	}

	if client.Authentication != nil {
		client.Authentication.Authenticate(req)
	}

	resp, err := httpClient.Do(req)

	if err != nil {
		return err
	}

	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		return fmt.Errorf("Unable to revoke access token, HTTP %v", resp.StatusCode)
	}

	client.Authentication = nil
	return nil
}

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
haveapi-go-client-0.26.0 template/authentication/oauth2.go.erb
haveapi-go-client-0.25.0 template/authentication/oauth2.go.erb
haveapi-go-client-0.24.0 template/authentication/oauth2.go.erb
haveapi-go-client-0.23.7 template/authentication/oauth2.go.erb
haveapi-go-client-0.23.6 template/authentication/oauth2.go.erb
haveapi-go-client-0.23.5 template/authentication/oauth2.go.erb
haveapi-go-client-0.23.4 template/authentication/oauth2.go.erb
haveapi-go-client-0.23.3 template/authentication/oauth2.go.erb
haveapi-go-client-0.23.2 template/authentication/oauth2.go.erb
haveapi-go-client-0.23.1 template/authentication/oauth2.go.erb
haveapi-go-client-0.23.0 template/authentication/oauth2.go.erb
haveapi-go-client-0.22.1 template/authentication/oauth2.go.erb
haveapi-go-client-0.22.0 template/authentication/oauth2.go.erb
haveapi-go-client-0.21.1 template/authentication/oauth2.go.erb
haveapi-go-client-0.21.0 template/authentication/oauth2.go.erb
haveapi-go-client-0.20.0 template/authentication/oauth2.go.erb