class Angus::Authentication::Provider
Constants
- AUTHENTICATION_HEADER
- BAAS_AUTHENTICATION_HEADER
- BAAS_SESSION_HEADER
- DATE_HEADER
- DEFAULT_ID_TTL
- DEFAULT_PRIVATE_KEY
- DEFAULT_SESSION_TTL
- DEFAULT_USE_SESSION
- PATH_HEADER
- REQUEST_HEADER
Public Class Methods
new(settings, env)
click to toggle source
# File lib/angus/authentication/provider.rb, line 24 def initialize(settings, env) @session_id_ttl = settings[:session_id_ttl] || DEFAULT_ID_TTL @session_ttl = settings[:session_ttl] || DEFAULT_SESSION_TTL @private_key = settings[:private_key] || DEFAULT_PRIVATE_KEY @use_session = settings[:use_session] @authenticator = settings[:authenticator] || DefaultAuthenticator.new(@private_key) @store = RedisStore.new(settings[:store] || {}) @excluded_regexps = settings[:excluded_regexps] || [] @env = env end
Public Instance Methods
authenticate!()
click to toggle source
# File lib/angus/authentication/provider.rb, line 35 def authenticate! return unless should_authenticate? if has_session? && use_session? authenticate_session else start_session end end
update_response_header(response)
click to toggle source
# File lib/angus/authentication/provider.rb, line 45 def update_response_header(response) return unless use_session? && should_authenticate? headers = response[1] headers[BAAS_SESSION_HEADER] = get_session_data['key_seed'] end
Private Instance Methods
auth_data()
click to toggle source
# File lib/angus/authentication/provider.rb, line 139 def auth_data "#{@env[DATE_HEADER]}\n" + "#{@env[REQUEST_HEADER]}\n" + "#{@env[PATH_HEADER]}" end
auth_token()
click to toggle source
# File lib/angus/authentication/provider.rb, line 145 def auth_token (@env[AUTHENTICATION_HEADER] || '').match(/.*:([a-zA-Z0-9]*)$/) $1 end
authenticate_session()
click to toggle source
# File lib/angus/authentication/provider.rb, line 85 def authenticate_session raise MissingAuthorizationData unless session_data_present? || authorization_data_present? if session_expired? && authorization_data_present? start_session elsif session_expired? raise AuthorizationTimeout elsif !valid_session_token? && authorization_data_present? start_session elsif !valid_session_token? raise InvalidAuthorizationData end end
extract_session_id(data)
click to toggle source
# File lib/angus/authentication/provider.rb, line 160 def extract_session_id(data) (data || '').match(/^([a-zA-Z0-9]*):.*/) $1 end
get_session_credentials()
click to toggle source
# File lib/angus/authentication/provider.rb, line 99 def get_session_credentials raise MissingAuthorizationData unless authorization_data_present? private_session_key, private_session_key_seed = @authenticator.call(session_id, auth_data, auth_token) raise InvalidAuthorizationData unless private_session_key return private_session_key, private_session_key_seed end
get_session_data()
click to toggle source
# File lib/angus/authentication/provider.rb, line 119 def get_session_data @store.get_session_data(session_id) end
has_session?()
click to toggle source
# File lib/angus/authentication/provider.rb, line 69 def has_session? @store.has_key?(session_id) end
request_path()
click to toggle source
# File lib/angus/authentication/provider.rb, line 65 def request_path @env[PATH_HEADER] end
session_auth_token()
click to toggle source
# File lib/angus/authentication/provider.rb, line 155 def session_auth_token (@env[BAAS_AUTHENTICATION_HEADER] || '').match(/.*:([a-zA-Z0-9]*)$/) $1 end
session_data_present?()
click to toggle source
# File lib/angus/authentication/provider.rb, line 128 def session_data_present? @env[DATE_HEADER] != nil && @env[BAAS_AUTHENTICATION_HEADER] != nil && extract_session_id(@env[BAAS_AUTHENTICATION_HEADER]) != nil end
session_expired?()
click to toggle source
# File lib/angus/authentication/provider.rb, line 133 def session_expired? created_at = Time.iso8601(get_session_data['created_at']) (created_at + @session_ttl) < Time.now end
session_id()
click to toggle source
# File lib/angus/authentication/provider.rb, line 150 def session_id extract_session_id(@env[BAAS_AUTHENTICATION_HEADER]) || extract_session_id(@env[AUTHENTICATION_HEADER]) end
set_session_data(session_data)
click to toggle source
# File lib/angus/authentication/provider.rb, line 110 def set_session_data(session_data) @store.save_session_data(session_id, session_data, @session_id_ttl + @session_ttl) end
should_authenticate?()
click to toggle source
# File lib/angus/authentication/provider.rb, line 59 def should_authenticate? return true if @excluded_regexps.empty? @excluded_regexps.none? { |regexp| request_path.match(regexp) } end
start_session()
click to toggle source
# File lib/angus/authentication/provider.rb, line 73 def start_session private_session_key, private_session_key_seed = get_session_credentials session_data = { 'private_key' => private_session_key, 'key_seed' => private_session_key_seed, 'created_at' => Time.now.iso8601 } set_session_data(session_data) end
use_session?()
click to toggle source
# File lib/angus/authentication/provider.rb, line 55 def use_session? @use_session || DEFAULT_USE_SESSION end
valid_session_token?()
click to toggle source
# File lib/angus/authentication/provider.rb, line 114 def valid_session_token? private_key = get_session_data['private_key'] Digest::SHA1.hexdigest("#{private_key}\n#{auth_data}") == session_auth_token end