shopifyoauth

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

README

go-shopify-oauth

OAuth Server for Shopify App.

How OAuth of Shopify App works

How to authenticate with OAuth for Shopify App is described in here.

In Short, OAuth client needs to provide two endpoints:

  1. Endpoint to provide App's detail

    • Provide requesting scope of App for authorization with a shop
  2. Endpoint to handle redirect after authorization

    • Receive AuthZ code so that requesting access token

Example

Runnable server example is in example.

Prerequisite

Following env var is required to run the example.

  • APP_NAME
    • Application's name
  • API_KEY
    • API key of Shopify App
  • API_SECRET
    • API Secret of Shopify App
  • SCOPE
    • OAuth Scope of Shopify App
  • GRANT_OPTION
  • SERVICE_URL
    • URL of the service

Installation Steps of Shopify App

  1. Create App in Shopify Partner dashboard
  2. Get API key and API Secret key from App Setup page and set them in env var
  3. Set env var of Scopes that an App needs
  4. Set env var of host name where your service will run
  5. Run the service
  6. Go to Test on development store in App page of Shopify Partner dashboard
  7. Select your test store and proceed installation

Documentation

Index

Constants

View Source
const (
	HMACParamKey      = "hmac"
	ShopParamKey      = "shop"
	TimestampParamKey = "timestamp"
	StateParamKey     = "state"
	HostParamKey      = "host"
	CodeParamKey      = "code"
	SignatureParamKey = "signature"
)
View Source
const AccessTokenURL = "https://%s/admin/oauth/access_token"

AccessTokenURL is the URL to extract access token of Shopify.

View Source
const AuthZURL = "https://%s/admin/oauth/authorize"
View Source
const DefaultDestinationURL = "https://%s/admin/apps"

DefaultDestinationURL is an URL of redirect target after App's installation.

View Source
const RedirectPath = "/oauth/redirect"

Variables

View Source
var ErrStateVerification = errors.New("value of OAuth state is not matched")
View Source
var ErrURLVerification = errors.New("url verification failed")

Functions

func DefaultAccessTokenHandle

func DefaultAccessTokenHandle(token string) error

DefaultAccessTokenHandle is a default handler of AccessToken.

Types

type AccessToken

type AccessToken struct {
	Code  string `json:"code"`
	Scope string `json:"scope"`
}

AccessToken is the response of oauth access_token endpoint.

type AccessTokenRequestPayload

type AccessTokenRequestPayload struct {
	// Client ID is the API key.
	ClientID string `url:"client_id"`
	// ClientSecret is the API secret.
	ClientSecret string `url:"client_secret"`
	// code is the authorization code provided in the redirect.
	Code string `url:"code"`
}

AccessTokenRequestPayload is the payload of oauth access_token endpoint.

type App

type App struct {
	AppName      string
	APIKey       string
	APISecret    string
	Scope        string
	GrantOptions string
}

type AuthorizeOption

type AuthorizeOption struct {
	// Client ID is the API key.
	ClientID string `url:"client_id"`
	// Scope is comma separated value of AuthZ scope in Shopify.
	Scope string `url:"scope"`
	// RedirectURI is a url redirected after authorizing client.
	RedirectURI string `url:"redirect_uri"`
	// State is nonce to keep the AuthN secure.
	State string `url:"state"`
	// GrantOptions is [Access Mode](https://shopify.dev/concepts/about-apis/authentication#api-access-modes).
	GrantOptions string `url:"grant_options,omitempty"`
}

AuthorizeOption is query params used on AuthZ redirect. [Shopify Docs](https://shopify.dev/tutorials/authenticate-with-oauth#step-2-ask-for-permission).

type Client

type Client struct {
	App          *goshopify.App
	ShopState    map[string]string
	HTTPClient   *http.Client
	TokenHandler TokenHandler
}

func NewClient

func NewClient(app *goshopify.App, th TokenHandle) *Client

func (Client) CacheState

func (c Client) CacheState(shop, state string)

func (*Client) DefaultAuthorizeHandler

func (c *Client) DefaultAuthorizeHandler() http.HandlerFunc

DefaultAuthorizeHandler is HTTP handler to respond AuthZ trigger to Shopify. A successful request is redirected to Shopify's App AuthZ view.

func (*Client) DefaultOAuthRedirectHandler

func (c *Client) DefaultOAuthRedirectHandler() http.HandlerFunc

func (*Client) GetAccessToken

func (c *Client) GetAccessToken(r *http.Request) (string, *ShopAccessTokenParam, error)

func (*Client) PrepareRedirect

func (c *Client) PrepareRedirect(r *http.Request) (string, error)

PrepareRedirect generates an URL of Shopify for user's AuthZ view. While generating an URL, keep nonce per shop ID for CSRF prevention.

func (*Client) VerifyRedirect added in v0.1.0

func (c *Client) VerifyRedirect(r *http.Request, p *ShopAccessTokenParam) (bool, error)

type OnlineModeAccessToken

type OnlineModeAccessToken struct {
	*AccessToken
	ExpiresIn string `json:"expires_in"`

	AssociatedUser struct {
		ID            string `json:"id"`
		FirstName     string `json:"first_name"`
		LastName      string `json:"last_name"`
		Email         string `json:"email"`
		EmailVerified bool   `json:"email_verified"`
		AccountOwner  bool   `json:"account_owner"`
		Locale        string `json:"locale"`
		Collaborator  bool   `json:"collaborator"`
	} `json:"associated_user"`
}

OnlineModeAccessToken is the response of oauth access_token endpoint when AuthZ was for online mode.

type ShopAccessTokenParam

type ShopAccessTokenParam struct {
	*ShopParam
	Nonce string
	Host  string
	Code  string
}

ShopAccessTokenParam is used to store params used in redirect request from Shopify.

type ShopParam

type ShopParam struct {
	HMAC      string `url:"hmac"`
	Shop      string `url:"shop"`
	TimeStamp string `url:"timestamp"`
}

ShopParam is params sent from Shopify on AuthZ process start.

type TokenHandle

type TokenHandle func(string) error

func (TokenHandle) Handle

func (th TokenHandle) Handle(token string) error

type TokenHandler

type TokenHandler interface {
	Handle(string) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL