Refactor handling of auth token for pre 0.9.9 clients
parent
a719251745
commit
47578b63b1
@ -0,0 +1,25 @@
|
||||
class ApiTokenRegistrator
|
||||
|
||||
def initialize(app)
|
||||
@app = app
|
||||
end
|
||||
|
||||
def call(env)
|
||||
auth = Rack::Auth::Basic::Request.new(env)
|
||||
|
||||
if auth.provided? && auth.basic? && auth.credentials
|
||||
ensure_user_with_token(*auth.credentials)
|
||||
end
|
||||
|
||||
@app.call(env)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ensure_user_with_token(username, token)
|
||||
unless ApiToken.where(token: token).exists?
|
||||
ApiToken.create_with_tmp_user!(token, username)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,26 @@
|
||||
class MetadataParser
|
||||
|
||||
def initialize(app)
|
||||
@app = app
|
||||
end
|
||||
|
||||
def call(env)
|
||||
request = Rack::Request.new(env)
|
||||
auth = Rack::Auth::Basic::Request.new(env)
|
||||
|
||||
if request.post? && request.path == '/api/asciicasts'
|
||||
if request.params['asciicast']['meta'] # pre "format 1" client
|
||||
meta = JSON.parse(request.params['asciicast']['meta'][:tempfile].read)
|
||||
request.params['asciicast']['meta'] = meta
|
||||
|
||||
username, token = meta.delete('username'), meta.delete('user_token')
|
||||
if token.present? && !auth.provided? || !auth.basic?
|
||||
env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(username, token)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@app.call(env)
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,24 @@
|
||||
class ApiTokenStrategy < ::Warden::Strategies::Base
|
||||
|
||||
def valid?
|
||||
auth.provided? && auth.basic? && auth.credentials
|
||||
end
|
||||
|
||||
def authenticate!
|
||||
user = User.for_api_token(auth.credentials.last)
|
||||
user.nil? ? fail!("Invalid auth token") : success!(user)
|
||||
end
|
||||
|
||||
def store?
|
||||
false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def auth
|
||||
@auth ||= Rack::Auth::Basic::Request.new(env)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Warden::Strategies.add(:api_token, ApiTokenStrategy)
|
Loading…
Reference in New Issue