File: session_helper.rb

package info (click to toggle)
ruby-devise-token-authenticatable 0.5.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 276 kB
  • sloc: ruby: 1,059; makefile: 2
file content (28 lines) | stat: -rw-r--r-- 1,053 bytes parent folder | download | duplicates (2)
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
# Helper methods for user sign in with
# authentication token.
#

# Signs in a user via different methods (i.e., HTTP AUTH,
# Token Auth, plain). If no user is given with the +options+
# a new one is created.
#
def sign_in_as_new_user_with_token(options = {})
  trait = options[:use] ? options[:use] : :with_authentication_token
  user = options.delete(:user) || create(:user, trait)

  options[:auth_token_key] ||= Devise::TokenAuthenticatable.token_authentication_key
  options[:auth_token]     ||= user.authentication_token

  if options[:http_auth]
    header = "Basic #{Base64.encode64("#{options[:auth_token]}:X")}"
    get users_path(format: :xml), {}, "HTTP_AUTHORIZATION" => header
  elsif options[:token_auth]
    token_options = options[:token_options] || {}
    header = ActionController::HttpAuthentication::Token.encode_credentials(options[:auth_token], token_options)
    get users_path(format: :xml), {}, "HTTP_AUTHORIZATION" => header
  else
    get users_path(options[:auth_token_key].to_sym => options[:auth_token])
  end

  user
end