module Sphinx::Oauth

Constants

VERSION

Public Class Methods

generateAccessToken(email, password, authorization, grant_type) click to toggle source
# File lib/sphinx/oauth.rb, line 5
def self.generateAccessToken(email, password, authorization, grant_type)
 if(grant_type.present? && grant_type == "password")
 if(checkValidRequest(authorization) && email.present? && password.present?)
    user = User.find_by_email(email)
        crypt = ActiveSupport::MessageEncryptor.new(Rails.application.config.SECRET_KEY)
 if(user.present? && comparePassword(crypt, user.password, password))
  oauthAccessToken = OauthAccessToken.find_by_user_name(email)
 if(!oauthAccessToken.present?)
        token = SecureRandom.uuid
        oauthAccessToken = OauthAccessToken.new
        token = SecureRandom.uuid
        refresh_token =  SecureRandom.uuid
    token_id = token.delete('-')
    oauthAccessToken.token_id = token_id.reverse
    oauthAccessToken.token = crypt.encrypt_and_sign(token)
    oauthAccessToken.user_name = user.email
    oauthAccessToken.client_id = Rails.application.config.CLIENT_ID
    oauthAccessToken.authentication_id = crypt.encrypt_and_sign((Time.now + Rails.application.config.ACCESS_VALID).to_s(:number))
    oauthAccessToken.refresh_token = refresh_token
    oauthAccessToken.authentication = crypt.encrypt_and_sign(user.id.to_s + ":" + user.role)
    oauthAccessToken.save
      else
    token = crypt.decrypt_and_verify(oauthAccessToken.token) 
        refresh_token = oauthAccessToken.refresh_token
  end         
    
   return :json => {
    "access_token": token,
    "token_type": "bearer",
    "refresh_token": refresh_token,
    "expires_in": Rails.application.config.ACCESS_VALID,
    "scope": "read write"
    }.to_json
        
end
   return :json => { :errors => "Invalid username and password"}.to_json, :status => 401
end
   return :json => { :errors => "Invalid request"}.to_json, :status => 401
end
    return :json => { :errors => "Invalid grant type"}.to_json, :status => 401 
end
getPrincipal(authorization) click to toggle source
# File lib/sphinx/oauth.rb, line 47
def self.getPrincipal(authorization)
   if authorization.present?
      authorization = authorization.split(" ")
              if authorization[0].present? &&  authorization[0].casecmp("bearer") == 0 && authorization[1].present?
              crypt = ActiveSupport::MessageEncryptor.new(Rails.application.config.SECRET_KEY)
      token_id = authorization[1].delete('-')
      token_id = token_id.reverse
                oauthAccessToken = OauthAccessToken.find_by_token_id(token_id)
                  puts "Roshan 1" + token_id
                if oauthAccessToken.present?
                 authentication = crypt.decrypt_and_verify(oauthAccessToken.authentication)
                 expires_in = crypt.decrypt_and_verify(oauthAccessToken.authentication_id)
                 if !checkTokenExpires(expires_in)
                   authentication = authentication.split(":") 
                      if authentication[1].present?
                   user = User.new
                   user.id = authentication[0]
                   user.role = authentication[1]
                       return user
                       end
           end          
          end
        end
      end
      return
end

Private Class Methods

checkTokenExpires(expires_in) click to toggle source
# File lib/sphinx/oauth.rb, line 80
def self.checkTokenExpires(expires_in)
 return Time.now.to_s(:number) > expires_in
end
checkValidRequest(authorization) click to toggle source
# File lib/sphinx/oauth.rb, line 84
def self.checkValidRequest(authorization)
if(authorization.present?)
authorization = authorization.split(" ")
 return authorization[0].present? &&  authorization[0].casecmp("basic") == 0 && authorization[1].present? && Base64.decode64(authorization[1].strip) ==  (Rails.application.config.CLIENT_ID + ":" + Rails.application.config.CLIENT_SECRET)
 end
  return false
end
comparePassword(crypt, userPassword, password) click to toggle source
# File lib/sphinx/oauth.rb, line 75
def self.comparePassword(crypt, userPassword, password)
 user_password = crypt.decrypt_and_verify(userPassword)
 return user_password == password
end