-
Notifications
You must be signed in to change notification settings - Fork 474
/
session.rb
161 lines (136 loc) · 4.83 KB
/
session.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# typed: strict
# frozen_string_literal: true
module ShopifyAPI
module Auth
class Session
extend T::Sig
sig { returns(String) }
attr_reader :id
sig { returns(T.nilable(String)) }
attr_accessor :state, :access_token
sig { returns(String) }
attr_accessor :shop
sig { returns(AuthScopes) }
attr_accessor :scope
sig { returns(T.nilable(AuthScopes)) }
attr_accessor :associated_user_scope
sig { returns(T.nilable(Time)) }
attr_accessor :expires
sig { returns(T.nilable(AssociatedUser)) }
attr_accessor :associated_user
sig { returns(T.nilable(String)) }
attr_accessor :shopify_session_id
sig { returns(T::Boolean) }
def online?
@is_online
end
sig { returns(T::Boolean) }
def expired?
@expires ? @expires < Time.now : false
end
sig do
params(
shop: String,
id: T.nilable(String),
state: T.nilable(String),
access_token: T.nilable(String),
scope: T.any(T::Array[String], String),
associated_user_scope: T.nilable(T.any(T::Array[String], String)),
expires: T.nilable(Time),
is_online: T.nilable(T::Boolean),
associated_user: T.nilable(AssociatedUser),
shopify_session_id: T.nilable(String),
).void
end
def initialize(shop:, id: nil, state: nil, access_token: "", scope: [], associated_user_scope: nil, expires: nil,
is_online: nil, associated_user: nil, shopify_session_id: nil)
@id = T.let(id || SecureRandom.uuid, String)
@shop = shop
@state = state
@access_token = access_token
@scope = T.let(AuthScopes.new(scope), AuthScopes)
@associated_user_scope = T.let(
associated_user_scope.nil? ? nil : AuthScopes.new(associated_user_scope), T.nilable(AuthScopes)
)
@expires = expires
@associated_user = associated_user
@is_online = T.let(is_online || !associated_user.nil?, T::Boolean)
@shopify_session_id = shopify_session_id
end
class << self
extend T::Sig
sig do
params(shop: String, access_token: String,
blk: T.proc.params(arg0: Session).returns(T.untyped)).returns(T.untyped)
end
def temp(shop:, access_token:, &blk)
original_session = Context.active_session
temp_session = Session.new(shop: shop, access_token: access_token)
begin
Context.activate_session(temp_session)
yield temp_session
ensure
Context.activate_session(original_session)
end
end
sig { params(shop: String, access_token_response: Oauth::AccessTokenResponse).returns(Session) }
def from(shop:, access_token_response:)
is_online = access_token_response.online_token?
if is_online
associated_user = T.must(access_token_response.associated_user)
expires = Time.now + access_token_response.expires_in.to_i
associated_user_scope = access_token_response.associated_user_scope
id = "#{shop}_#{associated_user.id}"
else
id = "offline_#{shop}"
end
new(
id: id,
shop: shop,
access_token: access_token_response.access_token,
scope: access_token_response.scope,
is_online: is_online,
associated_user_scope: associated_user_scope,
associated_user: associated_user,
expires: expires,
shopify_session_id: access_token_response.session,
)
end
sig { params(str: String).returns(Session) }
def deserialize(str)
Oj.load(str)
end
end
sig { params(other: Session).returns(Session) }
def copy_attributes_from(other)
JSON.parse(other.serialize).keys.each do |key|
next if key.include?("^")
variable_name = "@#{key}"
instance_variable_set(variable_name, other.instance_variable_get(variable_name))
end
self
end
sig { returns(String) }
def serialize
Oj.dump(self)
end
alias_method :eql?, :==
sig { params(other: T.nilable(Session)).returns(T::Boolean) }
def ==(other)
if other
id == other.id &&
shop == other.shop &&
state == other.state &&
scope == other.scope &&
associated_user_scope == other.associated_user_scope &&
(!(expires.nil? ^ other.expires.nil?) && (expires.nil? || expires.to_i == other.expires.to_i)) &&
online? == other.online? &&
associated_user == other.associated_user &&
shopify_session_id == other.shopify_session_id
else
false
end
end
end
end
end