forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add current_user_provider so people can override current_user bevior …
- Loading branch information
1 parent
8e6ae0e
commit 7993845
Showing
15 changed files
with
178 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Auth; end | ||
class Auth::CurrentUserProvider | ||
|
||
# do all current user initialization here | ||
def initialize(env) | ||
raise NotImplementedError | ||
end | ||
|
||
# our current user, return nil if none is found | ||
def current_user | ||
raise NotImplementedError | ||
end | ||
|
||
# log on a user and set cookies and session etc. | ||
def log_on_user(user,session,cookies) | ||
raise NotImplementedError | ||
end | ||
|
||
# api has special rights return true if api was detected | ||
def is_api? | ||
raise NotImplementedError | ||
end | ||
|
||
# we may need to know very early on in the middleware if an auth token | ||
# exists, to optimise caching | ||
def has_auth_cookie? | ||
raise NotImplementedError | ||
end | ||
|
||
|
||
def log_off_user(session, cookies) | ||
raise NotImplementedError | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
require_dependency "auth/current_user_provider" | ||
|
||
class Auth::DefaultCurrentUserProvider | ||
|
||
CURRENT_USER_KEY = "_DISCOURSE_CURRENT_USER" | ||
API_KEY = "_DISCOURSE_API" | ||
|
||
TOKEN_COOKIE = "_t" | ||
|
||
# do all current user initialization here | ||
def initialize(env) | ||
@env = env | ||
@request = Rack::Request.new(env) | ||
end | ||
|
||
# our current user, return nil if none is found | ||
def current_user | ||
return @env[CURRENT_USER_KEY] if @env.key?(CURRENT_USER_KEY) | ||
|
||
request = Rack::Request.new(@env) | ||
|
||
auth_token = request.cookies[TOKEN_COOKIE] | ||
|
||
current_user = nil | ||
|
||
if auth_token && auth_token.length == 32 | ||
current_user = User.where(auth_token: auth_token).first | ||
end | ||
|
||
if current_user && current_user.is_banned? | ||
current_user = nil | ||
end | ||
|
||
if current_user | ||
current_user.update_last_seen! | ||
current_user.update_ip_address!(request.ip) | ||
end | ||
|
||
# possible we have an api call, impersonate | ||
unless current_user | ||
if api_key = request["api_key"] | ||
if api_username = request["api_username"] | ||
if SiteSetting.api_key_valid?(api_key) | ||
@env[API_KEY] = true | ||
current_user = User.where(username_lower: api_username.downcase).first | ||
end | ||
end | ||
end | ||
end | ||
|
||
@env[CURRENT_USER_KEY] = current_user | ||
end | ||
|
||
def log_on_user(user, session, cookies) | ||
unless user.auth_token && user.auth_token.length == 32 | ||
user.auth_token = SecureRandom.hex(16) | ||
user.save! | ||
end | ||
cookies.permanent[TOKEN_COOKIE] = { value: user.auth_token, httponly: true } | ||
@env[CURRENT_USER_KEY] = user | ||
end | ||
|
||
def log_off_user(session, cookies) | ||
cookies[TOKEN_COOKIE] = nil | ||
end | ||
|
||
|
||
# api has special rights return true if api was detected | ||
def is_api? | ||
current_user | ||
@env[API_KEY] | ||
end | ||
|
||
def has_auth_cookie? | ||
request = Rack::Request.new(@env) | ||
cookie = request.cookies[CURRENT_USER_KEY] | ||
!cookie.nil? && cookie.length == 32 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,39 @@ | ||
module CurrentUser | ||
|
||
def self.has_auth_cookie?(env) | ||
request = Rack::Request.new(env) | ||
cookie = request.cookies["_t"] | ||
!cookie.nil? && cookie.length == 32 | ||
Discourse.current_user_provider.new(env).has_auth_cookie? | ||
end | ||
|
||
def self.lookup_from_env(env) | ||
request = Rack::Request.new(env) | ||
lookup_from_auth_token(request.cookies["_t"]) | ||
Discourse.current_user_provider.new(env).current_user | ||
end | ||
|
||
def self.lookup_from_auth_token(auth_token) | ||
if auth_token && auth_token.length == 32 | ||
User.where(auth_token: auth_token).first | ||
end | ||
end | ||
|
||
# can be used to pretend current user does no exist, for CSRF attacks | ||
def clear_current_user | ||
@current_user = nil | ||
@not_logged_in = true | ||
@current_user_provider = Discourse.current_user_provider.new({}) | ||
end | ||
|
||
def log_on_user(user) | ||
session[:current_user_id] = user.id | ||
unless user.auth_token && user.auth_token.length == 32 | ||
user.auth_token = SecureRandom.hex(16) | ||
user.save! | ||
end | ||
set_permanent_cookie!(user) | ||
current_user_provider.log_on_user(user,session,cookies) | ||
end | ||
|
||
def set_permanent_cookie!(user) | ||
cookies.permanent["_t"] = { value: user.auth_token, httponly: true } | ||
def log_off_user | ||
current_user_provider.log_off_user(session,cookies) | ||
end | ||
|
||
def is_api? | ||
# ensure current user has been called | ||
# otherwise | ||
current_user | ||
@is_api | ||
current_user_provider.is_api? | ||
end | ||
|
||
def current_user | ||
return @current_user if @current_user || @not_logged_in | ||
|
||
if session[:current_user_id].blank? | ||
# maybe we have a cookie? | ||
@current_user = CurrentUser.lookup_from_auth_token(cookies["_t"]) | ||
session[:current_user_id] = @current_user.id if @current_user | ||
else | ||
@current_user ||= User.where(id: session[:current_user_id]).first | ||
|
||
# I have flip flopped on this (sam), if our permanent cookie | ||
# conflicts with our current session assume session is bust | ||
# kill it | ||
if @current_user && cookies["_t"] != @current_user.auth_token | ||
@current_user = nil | ||
end | ||
|
||
end | ||
|
||
if @current_user && @current_user.is_banned? | ||
@current_user = nil | ||
end | ||
|
||
@not_logged_in = session[:current_user_id].blank? | ||
if @current_user | ||
@current_user.update_last_seen! | ||
@current_user.update_ip_address!(request.remote_ip) | ||
end | ||
current_user_provider.current_user | ||
end | ||
|
||
# possible we have an api call, impersonate | ||
unless @current_user | ||
if api_key = request["api_key"] | ||
if api_username = request["api_username"] | ||
if SiteSetting.api_key_valid?(api_key) | ||
@is_api = true | ||
@current_user = User.where(username_lower: api_username.downcase).first | ||
end | ||
end | ||
end | ||
end | ||
private | ||
|
||
@current_user | ||
def current_user_provider | ||
@current_user_provider ||= Discourse.current_user_provider.new(request.env) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters