Class: ShopifyAPI::Auth::JwtPayload

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/shopify_api/auth/jwt_payload.rb

Constant Summary collapse

JWT_EXPIRATION_LEEWAY =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ JwtPayload

Returns a new instance of JwtPayload.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/shopify_api/auth/jwt_payload.rb', line 18

def initialize(token)
  begin
    payload_hash = JWT.decode(token, Context.api_secret_key, true,
      { exp_leeway: JWT_EXPIRATION_LEEWAY, algorithm: "HS256" })[0]
  rescue
    raise ShopifyAPI::Errors::InvalidJwtTokenError, "Failed to parse session token '#{token}'"
  end

  @iss = T.let(payload_hash["iss"], String)
  @dest = T.let(payload_hash["dest"], String)
  @aud = T.let(payload_hash["aud"], String)
  @sub = T.let(payload_hash["sub"], String)
  @exp = T.let(payload_hash["exp"], Integer)
  @nbf = T.let(payload_hash["nbf"], Integer)
  @iat = T.let(payload_hash["iat"], Integer)
  @jti = T.let(payload_hash["jti"], String)
  @sid = T.let(payload_hash["sid"], String)

  raise ShopifyAPI::Errors::InvalidJwtTokenError,
    "Session token had invalid API key" unless @aud == Context.api_key
  raise ShopifyAPI::Errors::InvalidJwtTokenError,
    "Session token had invalid shop" unless validate_shop(shop)
end

Instance Attribute Details

#aud ⇒ Object (readonly)

Returns the value of attribute aud.



12
13
14
# File 'lib/shopify_api/auth/jwt_payload.rb', line 12

def aud
  @aud
end

#dest ⇒ Object (readonly)

Returns the value of attribute dest.



12
13
14
# File 'lib/shopify_api/auth/jwt_payload.rb', line 12

def dest
  @dest
end

#exp ⇒ Object (readonly)

Returns the value of attribute exp.



15
16
17
# File 'lib/shopify_api/auth/jwt_payload.rb', line 15

def exp
  @exp
end

#iat ⇒ Object (readonly)

Returns the value of attribute iat.



15
16
17
# File 'lib/shopify_api/auth/jwt_payload.rb', line 15

def iat
  @iat
end

#iss ⇒ Object (readonly)

Returns the value of attribute iss.



12
13
14
# File 'lib/shopify_api/auth/jwt_payload.rb', line 12

def iss
  @iss
end

#jti ⇒ Object (readonly)

Returns the value of attribute jti.



12
13
14
# File 'lib/shopify_api/auth/jwt_payload.rb', line 12

def jti
  @jti
end

#nbf ⇒ Object (readonly)

Returns the value of attribute nbf.



15
16
17
# File 'lib/shopify_api/auth/jwt_payload.rb', line 15

def nbf
  @nbf
end

#sid ⇒ Object (readonly)

Returns the value of attribute sid.



12
13
14
# File 'lib/shopify_api/auth/jwt_payload.rb', line 12

def sid
  @sid
end

#sub ⇒ Object (readonly)

Returns the value of attribute sub.



12
13
14
# File 'lib/shopify_api/auth/jwt_payload.rb', line 12

def sub
  @sub
end

Instance Method Details

#==(other) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/shopify_api/auth/jwt_payload.rb', line 54

def ==(other)
  return false unless other

  iss == other.iss &&
    dest == other.dest &&
    aud == other.aud &&
    sub == other.sub &&
    exp == other.exp &&
    nbf == other.nbf &&
    iat == other.iat &&
    jti == other.jti &&
    sid == other.sid
end

#eql? ⇒ Object



52
# File 'lib/shopify_api/auth/jwt_payload.rb', line 52

alias_method :eql?, :==

#shop ⇒ Object



43
44
45
# File 'lib/shopify_api/auth/jwt_payload.rb', line 43

def shop
  @dest.gsub("https://", "")
end

#validate_shop(shop) ⇒ Object



48
49
50
# File 'lib/shopify_api/auth/jwt_payload.rb', line 48

def validate_shop(shop)
  /\A[a-z0-9]+[a-z0-9\-]*[a-z0-9]+\.myshopify\.(io|com)\z/.match?(shop)
end