shoauth

package module
v0.0.0-...-8794003 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2015 License: MIT Imports: 12 Imported by: 0

README

shoauth

shoauth is an HTTP package for Go that implements the Shopify oauth2 API.

Features
  • Performs Shopify app installation (displaying auth page and retrieving access tokens)
  • Optionally performs webhook installation upon app installation
  • Performs Shopify request verification (both user & webhook)
  • Acts as typical Go middleware - meaning it can be placed in any middleware stack as long as it is made up of http.Handler
  • Allows custom failure handlers.
  • Has no dependencies external to the Go standard library.
Example
package main

import (
	"fmt"
	"github.com/darrenpeters/shoauth"
	"net/http"
	"os"
)

type dummyPersistence struct {}

func (p *dummyPersistence) InstallationExists(shopID string) bool {
	return false // Use any ORM or SQL package directly
}

func (p *dummyPersistence) CreateInstallation(shopID string, accessToken string) error {
	return nil // Use any ORM or SQL package directly
}

func (p *dummyPersistence) HasValidSession(r *http.Request) bool {
	return false // Use gorilla sessions or whatever here
}

func oauthConfig(s *shoauth.ShopifyConfig) {
	s.ClientID = os.Getenv("SHOPIFY_CLIENT_ID")
	s.SharedSecret = os.Getenv("SHOPIFY_SHARED_SECRET")
	s.IsEmbedded = true
	s.Scopes = []string{"read_products", "write_products", "read_customers", "write_customers", "read_orders", "write_orders", "read_shipping", "write_shipping"}
	s.Webhooks = make(map[string]string)
	s.Webhooks["orders/fulfilled"] = "https://yourapp.com/orders/fulfilled"
	s.Webhooks["app/uninstalled"] = "https://yourapp.com/app/uninstalled"
}

func main() {
	shoauthHandler := shoauth.NewShopifyOauthHandler(http.DefaultServeMux, shoauth.DefaultFailureHandler(), &dummyPersistence{}, oauthConfig)
	fmt.Println("Listening on http://127.0.0.1:8000/")
	http.ListenAndServe(":8000", shoauthHandler)
}
Documentation

Documentation is available at GoDoc

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInstallationExists is returned if a shop that has already installed this
	// application attempts to install it a second time.
	ErrInstallationExists = errors.New("An installation already exists for this shop and application.")
	// ErrInvalidHMAC is returned when a request from shopify (or pretending to
	// be from shopify) contains an invalid HMAC signature.
	ErrInvalidHMAC = errors.New("The request contained an invalid HMAC - this request will be discarded.")
	// ErrInvalidRequestData is returned when a json.Marshal fails on the
	// shopify access token request struct.
	ErrInvalidRequestData = errors.New("The request data to be sent to shopify for an access token was invalid - please make sure your configuration settings are applied properly.")
	// ErrInvalidResponseData is returned when a json.Unmarshal fails on a
	// response from shopify
	ErrInvalidResponseData = errors.New("The response data returned from shopify was in an unexpected format.")
	// ErrBadPersistence is returned when the implementation of persistence
	// passed to shoauth fails for some reason.
	ErrBadPersistence = errors.New("The persistence passed to shoauth failed to perform an action.")
)

Functions

func DefaultFailureHandler

func DefaultFailureHandler() http.Handler

DefaultFailureHandler returns an HTTP handler that simply displays a 403.

func NewShopifyOauthHandler

func NewShopifyOauthHandler(successHandler http.Handler, failureHandler http.Handler, persistence ShopifyPersistence, configOptions ...func(*ShopifyConfig)) *shopifyOauthHandler

NewShopifyOauthHandler returns the middleware handler that handles Shopify oauth requests and responses. It will call successHandler.ServeHTTP on a successful installation or verification, and will call failureHandler.ServeHTTP on an unsuccessful installation or verification. The user must pass a shopifyPersistence-satisfying struct and any functions they wish to operate on the default config object.

Types

type ErrShopifyHTTPRequestFailed

type ErrShopifyHTTPRequestFailed struct {
	// contains filtered or unexported fields
}

ErrShopifyHTTPRequestFailed is returned when an HTTP request to a shopify shop fails.

func (*ErrShopifyHTTPRequestFailed) Error

type ShopifyConfig

type ShopifyConfig struct {
	ClientID     string              // Your app's API key
	SharedSecret string              // Your app's shared secret
	RedirectURI  string              // If you want a different URL other than the callback on installation, put it here
	Scopes       []string            // Your app's required scopes
	IsEmbedded   bool                // If your app is embedded
	Webhooks     map[string]string   // Webhooks that should be created on installation.
	Scripts      map[string][]string // Script tags that should be created on installation.  Map is map[event][]sources
}

ShopifyConfig is a structure that contains variables specific to the app that the developer is creating.

type ShopifyPersistence

type ShopifyPersistence interface {
	InstallationExists(shopID string) bool                      // Checks if a shop has installed an app
	CreateInstallation(shopID string, accessToken string) error // Stores the installation in your app's persistence
}

ShopifyPersistence contains functions that a client app must implement to help this middleware.

Jump to

Keyboard shortcuts

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