-
Notifications
You must be signed in to change notification settings - Fork 55
/
app.rb
186 lines (153 loc) · 5.08 KB
/
app.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# frozen_string_literal: true
$LOAD_PATH.push File.expand_path("../../../../lib", __FILE__)
require "jwt"
require "jwt_sessions"
require "sinatra"
require "sinatra/namespace"
JWTSessions.signing_key = "secret key"
JWTSessions.token_store = ENV["STORE_ADAPTER"] || "redis"
get "/" do
"Welcome to Sinatra app!"
end
namespace "/api/v1" do
include JWTSessions::Authorization
# rack headers standard
ACCESS_HEADER = "HTTP_#{JWTSessions.access_header.downcase.gsub(/-/,'_').upcase}"
REFRESH_HEADER = "HTTP_#{JWTSessions.refresh_header.downcase.gsub(/-/,'_').upcase}"
CSRF_HEADER = "HTTP_#{JWTSessions.csrf_header.downcase.gsub(/-/,'_').upcase}"
before do
content_type "application/json"
end
def request_headers
jwt_headers = {}
jwt_headers[JWTSessions.access_header] = request.env[ACCESS_HEADER] if request.env[ACCESS_HEADER]
jwt_headers[JWTSessions.refresh_header] = request.env[REFRESH_HEADER] if request.env[REFRESH_HEADER]
jwt_headers[JWTSessions.csrf_header] = request.env[CSRF_HEADER] if request.env[CSRF_HEADER]
jwt_headers
end
def request_cookies
request.cookies
end
def request_method
request.request_method
end
error JWTSessions::Errors::Unauthorized do
{ error: "Unauthorized" }.to_json
end
post "/login" do
access_payload = { key: "big access value" }
refresh_payload = { refresh_key: "small refresh value" }
session = JWTSessions::Session.new(
payload: access_payload,
refresh_payload: refresh_payload,
refresh_by_access_allowed: true
)
session.login.to_json
end
post "/refresh" do
authorize_refresh_request!
access_payload = payload.merge({ key: "reloaded access value" })
session = JWTSessions::Session.new(payload: access_payload, refresh_payload: payload)
session.refresh(found_token).to_json
end
post "/refresh_by_cookies" do
authorize_by_refresh_cookie!
access_payload = payload.merge({ key: "new access value" })
session = JWTSessions::Session.new(payload: access_payload, refresh_payload: payload)
session.refresh(found_token).to_json
end
post "/refresh_by_headers" do
authorize_by_refresh_header!
access_payload = payload.merge({ key: "a little shy access value" })
session = JWTSessions::Session.new(payload: access_payload, refresh_payload: payload)
session.refresh(found_token).to_json
end
post "/refresh_by_access" do
authorize_refresh_by_access_request!
access_payload = payload.merge({ key: "a big brave access value" })
session = JWTSessions::Session.new(
payload: access_payload,
refresh_payload: payload,
refresh_by_access_allowed: true
)
session.refresh_by_access_payload.to_json
end
post "/refresh_by_access_by_cookies" do
authorize_refresh_by_access_cookie!
access_payload = payload.merge({ key: "such many auth methods much wow access value" })
session = JWTSessions::Session.new(
payload: access_payload,
refresh_payload: payload,
refresh_by_access_allowed: true
)
session.refresh_by_access_payload.to_json
end
post "/refresh_by_access_by_headers" do
authorize_refresh_by_access_header!
access_payload = payload.merge({ key: "yet another access value" })
session = JWTSessions::Session.new(
payload: access_payload,
refresh_payload: payload,
refresh_by_access_allowed: true
)
session.refresh_by_access_payload.to_json
end
post "/login_namespaced" do
access_payload = { key: "hellooo" }
refresh_payload = { refresh_key: "im a humble value" }
namespace = "custom-namespace"
session = JWTSessions::Session.new(
payload: access_payload,
refresh_payload: refresh_payload,
namespace: namespace
)
session.login.to_json
end
post "/refresh_namespaced" do
authorize_refresh_request!
access_payload = payload.merge({ key: "reloaded hellooo" })
namespace = "custom-namespace"
session = JWTSessions::Session.new(
payload: access_payload,
refresh_payload: payload,
namespace: namespace
)
session.refresh(found_token).to_json
end
post "/login_namespaced_with_refresh_by_access_allowed" do
access_payload = { key: "guten tag" }
namespace = "custom-namespace"
session = JWTSessions::Session.new(
payload: access_payload,
namespace: namespace,
refresh_by_access_allowed: true
)
session.login.to_json
end
post "/refresh_by_access_namespaced" do
authorize_refresh_by_access_request!
access_payload = payload.merge({ key: "reloaded guten tag" })
namespace = "custom-namespace"
session = JWTSessions::Session.new(
payload: access_payload,
namespace: namespace,
refresh_by_access_allowed: true
)
session.refresh_by_access_payload.to_json
end
get "/payload" do
authorize_access_request!
payload.to_json
end
get "/payload_by_cookies" do
authorize_access_request_by_cookies!
payload.to_json
end
get "/payload_by_headers" do
authorize_access_request_by_headers!
payload.to_json
end
get "/payload_without_authorize" do
payload.to_json
end
end