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

.find_by_code_app(code, app) ⇒ Object



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

def self.find_by_code_app(code, app)
  app_id = app.is_a?(Integer) ? app : app.id
  auth_grant = self.where("code = ? AND application_id = ?", code, app_id).first
end

.find_by_refresh_app(refresh_token, application_id) ⇒ Object



77
78
79
# File 'app/models/opro/oauth/auth_grant.rb', line 77

def self.find_by_refresh_app(refresh_token, application_id)
  self.where("refresh_token = ? AND application_id = ?", refresh_token, application_id).first
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_or_create_by_user_app(user, app) ⇒ Object



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

def self.find_or_create_by_user_app(user, app)
  app_id = app.is_a?(Integer) ? app : app.id
  auth_grant  =   self.where(:user_id  => user.id, :application_id => app_id).first
  auth_grant  ||= begin
    auth_grant                = self.new
    auth_grant.user_id        = user.id
    auth_grant.application_id = app_id
    auth_grant.save
    auth_grant
  end
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

Instance Method Details

#can?(value) ⇒ Boolean

attr_accessible :code, :access_token, :refresh_token, :access_token_expires_at, :permissions, :user_id, :user, :application_id, :application

Returns:

  • (Boolean)


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

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

#default_permissionsObject

turns array of permissions into a hash

:write, :read

> true, read: true



57
58
59
# File 'app/models/opro/oauth/auth_grant.rb', line 57

def default_permissions
  ::Opro.request_permissions.each_with_object({}) {|element, hash| hash[element] = true }
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

#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



102
103
104
105
106
107
108
109
110
# File 'app/models/opro/oauth/auth_grant.rb', line 102

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

#refreshObject

generates tokens, expires_at



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

def refresh
  generate_tokens!
  generate_expires_at!
  self
end

#refresh!Object

generates tokens, expires_at and saves



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

def refresh!
  refresh
  save!
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



95
96
97
98
99
100
# File 'app/models/opro/oauth/auth_grant.rb', line 95

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

#update_permissions(permissions = default_permissions) ⇒ Object



73
74
75
# File 'app/models/opro/oauth/auth_grant.rb', line 73

def update_permissions(permissions = default_permissions)
  self.permissions = permissions and save if self.permissions != permissions
end