session

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GRANT_TYPE_TOKEN_EXCHANGE    string = "urn:ietf:params:oauth:grant-type:token-exchange"          // Indicates that token exchange is to be performed.
	SUBJECT_TOKEN_TYPE           string = "urn:ietf:params:oauth:token-type:id_token"                // Indicates that the subject token type is an ID token.
	REQUESTED_TOKEN_TYPE_ONLINE  string = "urn:shopify:params:oauth:token-type:online-access-token"  // For requesting online access tokens
	REQUESTED_TOKEN_TYPE_OFFLINE string = "urn:shopify:params:oauth:token-type:offline-access-token" // (Default) For requesting offline access tokens
)
View Source
const (
	SessionTokenAlgorithm string = "HS256" // The algorithm used to encode the JWT.
	SessionTokenType      string = "JWT"   // The (type) header parameter used by session token to declare the media type.
)

Session tokens use the JWT Format and contain information about the merchant that's currently using your embedded app. This is the header. The values in the header are constant and never change.

Variables

This section is empty.

Functions

func ExchangeToken

func ExchangeToken(client *goshopify.ShopifyClient, sessionToken string, opts *TokenExchangeOpts) error

Exchange Shopify Session Token for Access Token. This assumes that you have already run the `ShopifyValidateSession` middleware prior to this as this doesn't validates sessionToken.

Workflow:

(1) Check if access token exists in cache

(a) If exists,

  • Proceed

(b) If does not exist,

  • (i) Check if there is an access token against the shop in the database
  • (ii) If found, cache the relevant data.
  • (iii) If not found, trigger exchange flow.

(2) Save access token to context.

(3) Generate a ShopifyClient and save to context.

(4) Proceed to the next middleware.

Types

type AssociatedUser

type AssociatedUser struct {
	ID            int    `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"`
}

The Online Token exchange also returns the associated user

type ExchangeTokenBody

type ExchangeTokenBody struct {
	ClientID         string `json:"client_id"`          // The API key for the app.
	ClientSecret     string `json:"client_secret"`      // The client secret for the app.
	GrantType        string `json:"grant_type"`         // Value of `GRANT_TYPE_TOKEN_EXCHANGE`
	SubjectToken     string `json:"subject_token"`      // An ID token that represents the identity and active browser session of a merchant using the app.
	SubjectTokenType string `json:"subject_token_type"` // Value of `SUBJECT_TOKEN_TYPE`
	RequestTokenType string `json:"request_token_type"` // Value of either `REQUESTED_TOKEN_TYPE_ONLINE` or `REQUESTED_TOKEN_TYPE_OFFLINE`
}

Body to be sent for Exchange Token request

type ExchangeTokenError

type ExchangeTokenError string

Errors occured during Exchange Token flow

const (
	ExchangeTokenErrorCacheSetFailed ExchangeTokenError = "CACHE_SET_FAILED" // Failed to set cache
)

type OfflineExchangeTokenResponse

type OfflineExchangeTokenResponse struct {
	AccessToken string `json:"access_token"` // The offline access token. Permanent. Cannot be revoked unless app Secret is rotated. Store securely.
	Scope       string `json:"scope"`        // Scopes associated to the access token.
}

Response received when doing an offline token exchange

type OnlineExchangeTokenResponse

type OnlineExchangeTokenResponse struct {
	AccessToken         string          `json:"access_token"`          // The online access token. Expires. Does not require storing.
	Scope               string          `json:"scope"`                 // Scopes associated to the access token.
	ExpiresIn           string          `json:"expires_in"`            // Time of expiry of the access token.
	AssociatedUserScope string          `json:"associated_user_scope"` // Scopes for the particular user.
	AssociatedUser      *AssociatedUser `json:"associated_user"`       // The associated user that is using the token.
}

Response received when doing an online token exchange

type ShopifySessionTokenClaims

type ShopifySessionTokenClaims struct {
	Issuer      string `json:"iss"`  // The shop's admin domain
	Audience    string `json:"aud"`  // The client ID of the receiving app.
	Subject     string `json:"sub"`  // The User that the session token is intended for.
	ExpiresAt   string `json:"exp"`  // When the session token expires.
	NotBefore   string `json:"nbf"`  // When the session token activates.
	IssuedAt    string `json:"iat"`  // When the session token was issued.
	ID          string `json:"jti"`  // A secure random UUID.
	Destination string `json:"dest"` // The shop's domain.
	SessionID   string `json:"sid"`  // A unique session ID per user and app.
	Signature   string `json:"sig"`  // Shopify signature.
}

Session tokens use the JWT Format and contain information about the merchant that's currently using your embedded app. This is the payload.

func (ShopifySessionTokenClaims) Validate

func (s ShopifySessionTokenClaims) Validate(cfg config.Config) error

Validate method

type TokenExchangeOpts

type TokenExchangeOpts struct {
	CacheKey         string                  // The cache key to passed to the CacheGet function to retrieve the value.
	CacheGet         func(key string) string // Get token from the cache.
	PreExchangeHook  func() error            // Hook to run before the exchange token request is run. If cache is hit, this is skipped.
	PostExchangeHook func() error            // Hook to run after the exchange token request succeeds or fails. If cache is hit, this is skipped.
}

Token Exchange Options

type ValidateSessionError

type ValidateSessionError string

Validate Session errors

const (
	ValidateSessionErrorSanitizeSession               ValidateSessionError = "SANITIZE_SESSION"                 // Failed to sanitize session.
	ValidateSessionErrorSessionTokenAlgorithmMismatch ValidateSessionError = "SESSION_TOKEN_ALGORITHM_MISMATCH" // Session token algorithm (HS256) mismatch.
)

Jump to

Keyboard shortcuts

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