class Oauth2Authorization

**************************************************************************

**************************************************************************

Public Class Methods

api_call(params,env,endpoint) click to toggle source

Process each oauth api requests for required results.

# File lib/modules/models/oauth2_authorization.rb, line 33
def self.api_call(params,env,endpoint)
  if User.validate_params?(params,endpoint)
    case endpoint
    when "register"
      expected_response,response_message = Oauth2Client.register(params)
    when "request_token"
      expected_response,response_message = Oauth2Client.process_request(params,env,"code")
    when "authorize"
      expected_response,response_message = Oauth2Client.process_request(params,env,"authorize")
    when "access_token"
      expected_response,response_message = Oauth2Client.process_request(params,env,"token")
    when "token"
      expected_response,response_message = Owner.process_bearer_request(params,env,"bearer_token")
    when "invalidate_token"
      expected_response,response_message = Owner.process_bearer_request(params,env,"invalidate")
    end
    return expected_response,response_message
  else
    error = "Parameters missing or invalid."
    error_response = Oauth2Authorization.error_response(error) 
    return error_response,false
  end 
end
error_response(error) click to toggle source

Creates and returns the error response.

# File lib/modules/models/oauth2_authorization.rb, line 177
def self.error_response(error)
  error_response = {
    :error => "Unauthorized access",
    :description => error,
    :status => 401
  }
end

Public Instance Methods

api_response(redirect_uri) click to toggle source

Creates and returns the basic api response.

# File lib/modules/models/oauth2_authorization.rb, line 95
def api_response(redirect_uri)
  redirect_to_url = self.build_url(redirect_uri,"token") 
  self.refresh_access_token if self.expired? 
  return redirect_to_url
end
build_url(redirect_uri,type) click to toggle source

Creates and returns the redirect url.

# File lib/modules/models/oauth2_authorization.rb, line 187
def build_url(redirect_uri,type)
  path = redirect_uri.split('#',2).first if redirect_uri.include? "#"
  path = redirect_uri.split('?',2).first if redirect_uri.include? "?"
  case type
  when "token"
    return path + "?access_token=#{self.access_token}"
  when "code"
    return path + "?request_token=#{self.code}"
  end
end
create_access_token() click to toggle source

Creates and returns the access token.

# File lib/modules/models/oauth2_authorization.rb, line 118
def create_access_token
  hash = nil
  Songkick::OAuth2.generate_id do |token|
    hash = Songkick::OAuth2.hashify(token)         
  end
  return hash
end
create_code(client) click to toggle source

Creates and returns the request token code.

# File lib/modules/models/oauth2_authorization.rb, line 110
def create_code(client)
  Songkick::OAuth2.generate_id do |code|
      return code
  end
end
create_refresh_token(client) click to toggle source

Creates and returns the request token hash.

# File lib/modules/models/oauth2_authorization.rb, line 128
def create_refresh_token(client)
  verified_client = Oauth2Client.find_by_client_id(client.client_id)
  Songkick::OAuth2.generate_id do |refresh_token|
    if verified_client
        hash = Songkick::OAuth2.hashify(refresh_token)
      else
          hash = nil
        end
  end
    return hash
end
expired?() click to toggle source

Checks the expiry of access token.

# File lib/modules/models/oauth2_authorization.rb, line 156
def expired?
  return false unless expires_at
  expires_at < Time.now
end
generate_access_token() click to toggle source

Creates and returns the access token hash.

# File lib/modules/models/oauth2_authorization.rb, line 163
def generate_access_token
  self.access_token ||= self.create_access_token
  save && access_token
end
generate_code() click to toggle source

Creates and generates the request token code.

# File lib/modules/models/oauth2_authorization.rb, line 170
def generate_code
  self.code ||= self.create_code(client)
  save && code
end
get_token(owner,client, attributes = {}) click to toggle source

Creates and returns the basic oauth details.

# File lib/modules/models/oauth2_authorization.rb, line 59
def get_token(owner,client, attributes = {})
  return nil unless owner and client
  @instance = owner.oauth2_authorization(client,owner) ||
      Oauth2Authorization.new do |authorization|
          authorization.oauth2_resource_owner_id  = owner.id
          authorization.oauth2_client_id = client.id
      end
  case attributes[:response_type]
  when 'code'
    @instance.code ||= create_code(client)
  when 'token'
    @instance.access_token  ||= create_access_token
    @instance.refresh_token ||= create_refresh_token(client)
  end

  if @instance.expires_at.nil?        
    @instance.expires_at = attributes[:duration].present? ? Time.now + attributes[:duration].to_i : nil         
  elsif attributes[:invalidate]
    @instance.expires_at = Time.now
  end

  if @instance.scope.nil?
    @instance.scope = attributes[:scope].present? ? attributes[:scope] : nil        
  elsif attributes[:scope].present?
    @instance.scope += "," + attributes[:scope] unless @instance.scope.include? attributes[:scope]
  end

  @instance.save
  return @instance

  rescue Object => error
    raise error
end
in_scope?(request_scope) click to toggle source

Checks the presence of scope attribute value.

# File lib/modules/models/oauth2_authorization.rb, line 150
def in_scope?(request_scope)
  [*request_scope].all?(&scopes.method(:include?))
end
redirect(auth) click to toggle source

Creates and returns the redirect url basic path.

# File lib/modules/models/oauth2_authorization.rb, line 200
def redirect(auth)
  return auth.redirect_uri.split('#',2).first
end
refresh_access_token() click to toggle source

Refreshes the expired access token.

# File lib/modules/models/oauth2_authorization.rb, line 103
def refresh_access_token
  self.expires_at = Time.now + 3600   
  save
end
scopes() click to toggle source

Handles the scope attribute.

# File lib/modules/models/oauth2_authorization.rb, line 142
def scopes
  scopes = scope ? scope.split(/\s+/) : []
  scopes = attributes[:scope]
  Set.new(scopes).to_s
end