Commit 3fff1e87 by liyijie

Add oauth session controller

parent c99b71f8
require "rails_api_authentication/version" require "rails_api_authentication/version"
require "rails_api_authentication/user_error"
require "rails_api_authentication/configuration" require "rails_api_authentication/configuration"
require "rails_api_authentication/auth_action" require "rails_api_authentication/auth_action"
require "rails_api_authentication/auth_session" require "rails_api_authentication/auth_session"
require "rails_api_authentication/oauth_session"
require "rails_api_authentication/code_session" require "rails_api_authentication/code_session"
require "rails_api_authentication/auth_password" require "rails_api_authentication/auth_password"
require "rails_api_authentication/auth_token" require "rails_api_authentication/auth_token"
......
...@@ -12,6 +12,13 @@ module RailsApiAuthentication ...@@ -12,6 +12,13 @@ module RailsApiAuthentication
auth_session klass_sym auth_session klass_sym
end end
def acts_as_oauth_session(klass_sym)
include RailsApiAuthentication::AuthAction
include RailsApiAuthentication::OauthSession
auth_action klass_sym, only: [:destroy]
oauth_session klass_sym
end
def acts_as_auth_password(klass_sym) def acts_as_auth_password(klass_sym)
include RailsApiAuthentication::AuthAction include RailsApiAuthentication::AuthAction
include RailsApiAuthentication::AuthPassword include RailsApiAuthentication::AuthPassword
......
...@@ -72,6 +72,13 @@ module RailsApiAuthentication ...@@ -72,6 +72,13 @@ module RailsApiAuthentication
AuthToken.create(self, oauth_params(params).merge({ oid: user.id }) ) AuthToken.create(self, oauth_params(params).merge({ oid: user.id }) )
end end
def oauth_login(oauth_type, oauth_id)
user = self.find_or_create_by oauth_type: oauth_type, oauth_id: oauth_id
AuthToken.create(
self, {oid: user.id, oauth_type: oauth_type, oauth_id: oauth_id }
)
end
def auth!(request) def auth!(request)
token = request.env["HTTP_#{self.to_s.upcase}_TOKEN"] || request.env["#{self.to_s.upcase}_TOKEN"] token = request.env["HTTP_#{self.to_s.upcase}_TOKEN"] || request.env["#{self.to_s.upcase}_TOKEN"]
user = auth(token) user = auth(token)
......
module RailsApiAuthentication
module OauthSession
extend ActiveSupport::Concern
included do
end
def create
@auth_token = self.class.klass.oauth_login(session_params.delete(:oauth_type), session_params.delete(:oauth_id))
render json: { token: @auth_token.token }, status: 200
rescue UserError => e
render json: { error: e.message }, status: e.status
end
def destroy
self.send("current_#{self.class.klass_sym}")&.logout
render json: { message: "logout successful" }, status: 200
end
private 
def session_params
params.require(self.class.klass_sym).permit(:oauth_type, :oauth_id)
end
module ClassMethods
attr_reader :klass, :klass_sym
def oauth_session klass_sym
@klass = klass_sym.to_s.camelize.constantize
@klass_sym = klass_sym
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment