Class: Opro::Oauth::AuthGrant

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/opro/oauth/auth_grant.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.auth_with_code!(code, application_id) ⇒ Object



50
51
52
# File 'app/models/opro/oauth/auth_grant.rb', line 50

def self.auth_with_code!(code, application_id)
  auth_grant = self.where("code = ? AND application_id = ?", code, application_id).first
end

.auth_with_user!(user, applicaiton_id, permissions = ::Opro.request_permissions) ⇒ Object



54
55
56
57
58
59
60
61
# File 'app/models/opro/oauth/auth_grant.rb', line 54

def self.auth_with_user!(user, applicaiton_id, permissions = ::Opro.request_permissions)
  return false unless user
  permissions_hash =   permissions.each_with_object({}) {|element, hash| hash[element] = true }
  auth_grant  =   self.where(:user_id  => user.id, :application_id => applicaiton_id).first
  auth_grant  ||= self.create(:user_id => user.id, :application_id => applicaiton_id)
  auth_grant.update_attributes(:permissions => permissions_hash)
  auth_grant
end

.find_for_token(token) ⇒ Object



42
43
44
# File 'app/models/opro/oauth/auth_grant.rb', line 42

def self.find_for_token(token)
  self.where(:access_token => token).includes(:user, :client_application).first
end

.find_user_for_token(token) ⇒ Object



46
47
48
# File 'app/models/opro/oauth/auth_grant.rb', line 46

def self.find_user_for_token(token)
  find_app_for_token.try(:user)
end

.refresh_tokens!(refresh_token, application_id) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'app/models/opro/oauth/auth_grant.rb', line 63

def self.refresh_tokens!(refresh_token, application_id)
  auth_grant = self.where("refresh_token = ? AND application_id = ?", refresh_token, application_id).first
  if auth_grant.present?
    auth_grant.generate_tokens!
    auth_grant.generate_expires_at!
    auth_grant.save!
  end
  auth_grant
end

Instance Method Details

#can?(value) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'app/models/opro/oauth/auth_grant.rb', line 23

def can?(value)
  HashWithIndifferentAccess.new(permissions)[value]
end

#expired?Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'app/models/opro/oauth/auth_grant.rb', line 27

def expired?
  return false unless ::Opro.require_refresh_within.present?
  return expires_in && expires_in < 0
end

#expires_inObject



36
37
38
39
40
# File 'app/models/opro/oauth/auth_grant.rb', line 36

def expires_in
  return false unless access_token_expires_at.present?
  time = access_token_expires_at - Time.now
  time.to_i
end

#generate_expires_at!Object



73
74
75
76
77
78
79
80
# File 'app/models/opro/oauth/auth_grant.rb', line 73

def generate_expires_at!
  if ::Opro.require_refresh_within.present?
    self.access_token_expires_at = Time.now + ::Opro.require_refresh_within
  else
    self.access_token_expires_at = nil
  end
  true
end

#generate_tokens!Object



82
83
84
85
86
# File 'app/models/opro/oauth/auth_grant.rb', line 82

def generate_tokens!
  self.code          = unique_token_for(:code)
  self.access_token  = unique_token_for(:access_token)
  self.refresh_token = unique_token_for(:refresh_token)
end

#not_expired?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/models/opro/oauth/auth_grant.rb', line 32

def not_expired?
  !expired?
end

#redirect_uri_for(redirect_uri, state = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'app/models/opro/oauth/auth_grant.rb', line 96

def redirect_uri_for(redirect_uri, state = nil)
  if redirect_uri =~ /\?/
    redirect_uri << "&code=#{code}&response_type=code"
  else
    redirect_uri << "?code=#{code}&response_type=code"
  end
  redirect_uri << "&state=#{state}" if state.present?
  redirect_uri
end

#unique_token_for(field, secure_token = SecureRandom.hex(16)) ⇒ Object

used to guarantee that we are generating unique codes, access_tokens and refresh_tokens



89
90
91
92
93
94
# File 'app/models/opro/oauth/auth_grant.rb', line 89

def unique_token_for(field, secure_token  = SecureRandom.hex(16))
  raise "bad field" unless self.respond_to?(field)
  auth_grant = self.class.where(field => secure_token).first
  return secure_token if auth_grant.blank?
  unique_token_for(field)
end