Module: RocketIO::TokenAuth
Constant Summary collapse
- TOKEN_KEY =
'token='.freeze
- TOKEN_REGEX =
/^Token /- AUTHN_PAIR_DELIMITERS =
/(?:,|;|\t+)/- HTTP_AUTHORIZATION =
'HTTP_AUTHORIZATION'.freeze
- X_HTTP_AUTHORIZATION_I =
'X-HTTP_AUTHORIZATION'.freeze
- X_HTTP_AUTHORIZATION_II =
'X_HTTP_AUTHORIZATION'.freeze
- REDIRECT_X_HTTP_AUTHORIZATION =
'REDIRECT_X_HTTP_AUTHORIZATION'.freeze
- WWW_AUTHENTICATE =
'WWW-Authenticate'.freeze
- TOKEN_REALM_FORMAT =
'Token realm="%s"'.freeze
- ACCESS_DENIED =
"HTTP Token: Access denied.\n".freeze
Instance Method Summary collapse
-
#authenticate(env) {|token, options| ... } ⇒ Object
If token Authorization header is present, call the login procedure with the present token and options.
-
#authentication_request(realm) ⇒ Array
Sets a WWW-Authenticate to let the client know a token is desired.
-
#authorization?(env) ⇒ Boolean
Returns the authorization header regardless of whether it was specified directly or through one of the proxy alternatives.
-
#params_array_from(raw_params) ⇒ Array
Takes raw_params and turns it into an array of parameters.
-
#raw_params(auth) ⇒ Array
This method takes an authorization body and splits up the key-value pairs by the standardized ‘:`, `;`, or `t`.
-
#rewrite_param_values(array_params) ⇒ Object
This removes the ‘“` characters wrapping the value.
-
#token_and_options(env) ⇒ Array
Parses the token and options out of the token authorization header.
- #token_params_from(auth) ⇒ Object
Instance Method Details
#authenticate(env) {|token, options| ... } ⇒ Object
If token Authorization header is present, call the login procedure with the present token and options.
45 46 47 48 49 |
# File 'lib/rocketio/controller/token_auth.rb', line 45 def authenticate env token, = (env) return if token.nil? || token.empty? yield(token, ) end |
#authentication_request(realm) ⇒ Array
Sets a WWW-Authenticate to let the client know a token is desired.
110 111 112 113 114 115 116 |
# File 'lib/rocketio/controller/token_auth.rb', line 110 def authentication_request realm [ 401, {WWW_AUTHENTICATE => TOKEN_REALM_FORMAT % realm.tr('"', '')}, [ACCESS_DENIED] ] end |
#authorization?(env) ⇒ Boolean
Returns the authorization header regardless of whether it was specified directly or through one of the proxy alternatives.
67 68 69 70 71 72 |
# File 'lib/rocketio/controller/token_auth.rb', line 67 def env env[HTTP_AUTHORIZATION] || env[X_HTTP_AUTHORIZATION_I] || env[X_HTTP_AUTHORIZATION_II] || env[REDIRECT_X_HTTP_AUTHORIZATION] end |
#params_array_from(raw_params) ⇒ Array
Takes raw_params and turns it into an array of parameters
82 83 84 |
# File 'lib/rocketio/controller/token_auth.rb', line 82 def params_array_from raw_params raw_params.map { |param| param.split %r/=(.+)?/ } end |
#raw_params(auth) ⇒ Array
This method takes an authorization body and splits up the key-value pairs by the standardized ‘:`, `;`, or `t`
98 99 100 101 102 103 104 |
# File 'lib/rocketio/controller/token_auth.rb', line 98 def raw_params auth _raw_params = auth.sub(TOKEN_REGEX, '').split(/\s*#{AUTHN_PAIR_DELIMITERS}\s*/) unless _raw_params.first =~ %r{\A#{TOKEN_KEY}} _raw_params[0] = [TOKEN_KEY, _raw_params.first]*'' end _raw_params end |
#rewrite_param_values(array_params) ⇒ Object
This removes the ‘“` characters wrapping the value.
89 90 91 |
# File 'lib/rocketio/controller/token_auth.rb', line 89 def rewrite_param_values array_params array_params.each { |param| (param[1] || "").gsub! %r/^"|"$/, '' } end |
#token_and_options(env) ⇒ Array
Parses the token and options out of the token authorization header. If the header looks like this:
Authorization: Token token="abc", nonce="def"
Then the returned token is “abc”, and the options is “def”
58 59 60 61 62 63 |
# File 'lib/rocketio/controller/token_auth.rb', line 58 def env return unless = (env) return unless [TOKEN_REGEX] params = token_params_from() [params.shift[1], RocketIO.indifferent_params(Hash[params])] end |
#token_params_from(auth) ⇒ Object
74 75 76 |
# File 'lib/rocketio/controller/token_auth.rb', line 74 def token_params_from auth rewrite_param_values(params_array_from(raw_params(auth))) end |