module Signet::OAuth2

An implementation of tools.ietf.org/html/draft-ietf-oauth-v2-10

This module will be updated periodically to support newer drafts of the specification, as they become widely deployed.

Public Class Methods

generate_authorization_uri(authorization_uri, parameters={}) click to toggle source

Appends the necessary OAuth parameters to the base authorization endpoint URI.

@param [Addressable::URI, String, to_str] authorization_uri

The base authorization endpoint URI.

@return [String] The authorization URI to redirect the user to.

# File lib/signet/oauth_2.rb, line 138
def self.generate_authorization_uri(authorization_uri, parameters={})
  for key, value in parameters
    parameters.delete(key) if value == nil
  end
  parsed_uri = Addressable::URI.parse(authorization_uri).dup
  query_values = parsed_uri.query_values || {}
  query_values = query_values.merge(parameters)
  parsed_uri.query_values = query_values
  return parsed_uri.normalize.to_s
end
generate_basic_authorization_header(client_id, client_password) click to toggle source

Generates a Basic Authorization header from a client identifier and a client password.

@param [String] client_id

The client identifier.

@param [String] client_password

The client password.

@return [String]

The value for the HTTP Basic Authorization header.
# File lib/signet/oauth_2.rb, line 95
def self.generate_basic_authorization_header(client_id, client_password)
  if client_id =~ %r:/
    raise ArgumentError,
      "A client identifier may not contain a ':' character."
  end
  return 'Basic ' + Base64.encode64(
    client_id + ':' + client_password
  ).gsub(%r\n/, '')
end
generate_bearer_authorization_header( access_token, auth_params=nil) click to toggle source

Generates an authorization header for an access token

@param [String] access_token

The access token.

@param [Hash] auth_params

Additonal parameters to be encoded in the header

@return [String]

The value for the HTTP Basic Authorization header.
# File lib/signet/oauth_2.rb, line 115
def self.generate_bearer_authorization_header(
    access_token, auth_params=nil)
  # TODO: escaping?
  header = "Bearer #{access_token}"
  if auth_params && !auth_params.empty?
    header += (", " +
      (auth_params.inject([]) do |accu, (key, value)|
        accu << "#{key}=\"#{value}\""
        accu
      end).join(", ")
    )
  end
  return header
end
parse_authorization_header(field_value) click to toggle source
# File lib/signet/oauth_2.rb, line 28
def self.parse_authorization_header(field_value)
  auth_scheme = field_value[%r^([-._0-9a-zA-Z]+)/, 1]
  case auth_scheme
  when %r^Basic$/
    # HTTP Basic is allowed in OAuth 2
    return self.parse_basic_credentials(field_value[%r^Basic\s+(.*)$/, 1])
  when %r^OAuth$/
    # Other token types may be supported eventually
    return self.parse_bearer_credentials(field_value[%r^OAuth\s+(.*)$/, 1])
  else
    raise ParseError,
      'Parsing non-OAuth Authorization headers is out of scope.'
  end
end
parse_basic_credentials(credential_string) click to toggle source
# File lib/signet/oauth_2.rb, line 55
def self.parse_basic_credentials(credential_string)
  decoded = Base64.decode64(credential_string)
  client_id, client_secret = decoded.split(':', 2)
  return [['client_id', client_id], ['client_secret', client_secret]]
end
parse_bearer_credentials(credential_string) click to toggle source
# File lib/signet/oauth_2.rb, line 61
def self.parse_bearer_credentials(credential_string)
  access_token = credential_string[%r^([^,\s]+)(?:\s|,|$)/, 1]
  parameters = []
  parameters << ['access_token', access_token]
  auth_param_string = credential_string[%r^(?:[^,\s]+)\s*,\s*(.*)$/, 1]
  if auth_param_string
    # This code will rarely get called, but is included for completeness
    parameters.concat(Signet.parse_auth_param_list(auth_param_string))
  end
  return parameters
end
parse_json_credentials(body) click to toggle source
# File lib/signet/oauth_2.rb, line 77
def self.parse_json_credentials(body)
  if !body.kind_of?(String)
    raise TypeError, "Expected String, got #{body.class}."
  end
  return MultiJson.load(body)
end
parse_oauth_challenge(challenge_string) click to toggle source
# File lib/signet/oauth_2.rb, line 73
def self.parse_oauth_challenge(challenge_string)
  return Signet.parse_auth_param_list(challenge_string)
end
parse_www_authenticate_header(field_value) click to toggle source
# File lib/signet/oauth_2.rb, line 43
def self.parse_www_authenticate_header(field_value)
  auth_scheme = field_value[%r^([-._0-9a-zA-Z]+)/, 1]
  case auth_scheme
  when %r^OAuth$/
    # Other token types may be supported eventually
    return self.parse_oauth_challenge(field_value[%r^OAuth\s+(.*)$/, 1])
  else
    raise ParseError,
      'Parsing non-OAuth WWW-Authenticate headers is out of scope.'
  end
end