forked from simontaen/SpotifyTokenSwap
-
Notifications
You must be signed in to change notification settings - Fork 27
/
spotify_token_swap_service.rb
227 lines (188 loc) Β· 5.61 KB
/
spotify_token_swap_service.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
require "sinatra"
require "sinatra/json"
require "sinatra/reloader" if File.exists?(".env")
require "dotenv/load" if File.exists?(".env")
require "active_support/all"
require "base64"
require "encrypted_strings"
require "singleton"
require "httparty"
module SpotifyTokenSwapService
# SpotifyTokenSwapService::ConfigHelper
# SpotifyTokenSwapService::ConfigError
# SpotifyTokenSwapService::Config
#
# This deals with configuration, loaded through .env
#
module ConfigHelper
def config
@config ||= Config.instance
end
end
class ConfigError < StandardError
def self.empty
new("client credentials are empty")
end
end
class Config < Struct.new(:client_id, :client_secret,
:client_callback_url, :encryption_secret)
include Singleton
def initialize
self.client_id = ENV["SPOTIFY_CLIENT_ID"]
self.client_secret = ENV["SPOTIFY_CLIENT_SECRET"]
self.client_callback_url = ENV["SPOTIFY_CLIENT_CALLBACK_URL"]
self.encryption_secret = ENV["ENCRYPTION_SECRET"]
validate_client_credentials
end
def has_client_credentials?
client_id.present? &&
client_secret.present? &&
client_callback_url.present?
end
def has_encryption_secret?
encryption_secret.present?
end
private
def validate_client_credentials
raise ConfigError.empty unless has_client_credentials?
end
end
# SpotifyTokenSwapService::HTTP
#
# Make the HTTP requests, as handled by our lovely host, HTTParty.
#
class HTTP
include HTTParty,
ConfigHelper
base_uri "https://accounts.spotify.com"
def token(auth_code:)
options = default_options.deep_merge(query: {
grant_type: "authorization_code",
redirect_uri: config.client_callback_url,
code: auth_code
})
self.class.post("/api/token", options)
end
def refresh_token(refresh_token:)
options = default_options.deep_merge(query: {
grant_type: "refresh_token",
refresh_token: refresh_token
})
self.class.post("/api/token", options)
end
private
def default_options
{ headers: { Authorization: authorization_basic } }
end
def authorization_basic
"Basic %s" % Base64.strict_encode64("%s:%s" % [
config.client_id,
config.client_secret
])
end
end
# SpotifyTokenSwapService::EncryptionMiddleware
#
# The code needed to apply encryption middleware for refresh tokens.
#
class EncryptionMiddleware < Struct.new(:httparty_instance)
include ConfigHelper
def run
response = httparty_instance.parsed_response.with_indifferent_access
if response[:refresh_token].present?
response[:refresh_token] = encrypt_refresh_token(response[:refresh_token])
end
[httparty_instance.response.code.to_i, response]
end
private
def encrypt_refresh_token(refresh_token)
if config.has_encryption_secret?
refresh_token.encrypt(:symmetric, password: ENV["ENCRYPTION_SECRET"])
end || refresh_token
end
end
# SpotifyTokenSwapService::DecryptParameters
#
# The code needed to apply decryption middleware for refresh tokens.
#
class DecryptParameters < Struct.new(:params)
include ConfigHelper
def initialize(init_params)
self.params = init_params.with_indifferent_access
end
def refresh_token
params[:refresh_token].to_s.gsub("\\n", "\n")
end
def run
params.merge({
refresh_token: decrypt_refresh_token(refresh_token)
})
end
private
def decrypt_refresh_token(refresh_token)
if config.has_encryption_secret?
refresh_token.decrypt(:symmetric, password: ENV["ENCRYPTION_SECRET"])
end || refresh_token
end
end
# SpotifyTokenSwapService::EmptyMiddleware
#
# Similar to EncryptionMiddleware, but it does nothing except
# comply with our DSL for middleware - [status code, response]
#
class EmptyMiddleware < Struct.new(:httparty_instance)
include ConfigHelper
def run
response = httparty_instance.parsed_response.with_indifferent_access
[httparty_instance.response.code.to_i, response]
end
end
# SpotifyTokenSwapService::App
#
# The code needed to make it go all Sinatra, beautiful.
#
class App < Sinatra::Base
set :root, File.dirname(__FILE__)
before do
headers "Access-Control-Allow-Origin" => "*",
"Access-Control-Allow-Methods" => %w(OPTIONS GET POST)
end
helpers ConfigHelper
# POST /api/token
# Convert an authorization code to an access token.
#
# @param code The authorization code sent from accounts.spotify.com
#
post "/api/token" do
begin
http = HTTP.new.token(auth_code: params[:code])
status_code, response = EncryptionMiddleware.new(http).run
status status_code
json response
rescue StandardError => e
status 400
json error: e
end
end
# POST /api/refresh_token
# Use a refresh token to generate a one-hour access token.
#
# @param refresh_token The refresh token provided from /api/token
#
post "/api/refresh_token" do
begin
refresh_params = DecryptParameters.new(params).run
http = HTTP.new.refresh_token(refresh_token: refresh_params[:refresh_token])
status_code, response = EmptyMiddleware.new(http).run
status status_code
json response
rescue OpenSSL::Cipher::CipherError
status 400
json error: "invalid refresh_token"
rescue StandardError => e
status 400
json error: e
end
end
end
end