Class: RockOAuth::Model::Authorization

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
Hashing
Defined in:
lib/rockoauth/model/authorization.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hashing

hashes_attributes

Class Method Details

.create_access_tokenObject



32
33
34
35
36
37
# File 'lib/rockoauth/model/authorization.rb', line 32

def self.create_access_token
  RockOAuth.generate_id do |token|
    hash = RockOAuth.hashify(token)
    Helpers.count(self, :access_token_hash => hash).zero?
  end
end

.create_code(client) ⇒ Object



26
27
28
29
30
# File 'lib/rockoauth/model/authorization.rb', line 26

def self.create_code(client)
  RockOAuth.generate_id do |code|
    Helpers.count(client.authorizations, :code => code).zero?
  end
end

.create_refresh_token(client) ⇒ Object



39
40
41
42
43
44
# File 'lib/rockoauth/model/authorization.rb', line 39

def self.create_refresh_token(client)
  RockOAuth.generate_id do |refresh_token|
    hash = RockOAuth.hashify(refresh_token)
    Helpers.count(client.authorizations, :refresh_token_hash => hash).zero?
  end
end

.for(owner, client, attributes = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rockoauth/model/authorization.rb', line 46

def self.for(owner, client, attributes = {})
  return nil unless owner and client

  unless client.is_a?(Client)
    raise ArgumentError, "The argument should be a #{Client}, instead it was a #{client.class}"
  end

  instance = owner.oauth2_authorization_for(client) ||
    new do |authorization|
    authorization.owner  = owner
    authorization.client = client
  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)
  when CODE_AND_TOKEN
    instance.code = create_code(client)
    instance.access_token  ||= create_access_token
    instance.refresh_token ||= create_refresh_token(client)
  end

  if attributes[:duration]
    instance.expires_at = Time.now + attributes[:duration].to_i
  else
    instance.expires_at = nil
  end

  scopes = instance.scopes + (attributes[:scopes] || [])
  scopes += attributes[:scope].split(/\s+/) if attributes[:scope]
  instance.scope = scopes.empty? ? nil : scopes.entries.join(' ')

  instance.save && instance

rescue Object => error
  if Model.duplicate_record_error?(error)
    retry
  else
    raise error
  end
end

Instance Method Details

#exchange!Object



91
92
93
94
95
96
# File 'lib/rockoauth/model/authorization.rb', line 91

def exchange!
  self.code          = nil
  self.access_token  = self.class.create_access_token
  self.refresh_token = nil
  save!
end

#expired?Boolean

Returns:

  • (Boolean)


98
99
100
101
# File 'lib/rockoauth/model/authorization.rb', line 98

def expired?
  return false unless expires_at
  expires_at < Time.now
end

#expires_inObject



103
104
105
# File 'lib/rockoauth/model/authorization.rb', line 103

def expires_in
  expires_at && (expires_at - Time.now).ceil
end

#generate_access_tokenObject



112
113
114
115
# File 'lib/rockoauth/model/authorization.rb', line 112

def generate_access_token
  self.access_token ||= self.class.create_access_token
  save && access_token
end

#generate_codeObject



107
108
109
110
# File 'lib/rockoauth/model/authorization.rb', line 107

def generate_code
  self.code ||= self.class.create_code(client)
  save && code
end

#grants_access?(user, *scopes) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/rockoauth/model/authorization.rb', line 117

def grants_access?(user, *scopes)
  not expired? and user == owner and in_scope?(scopes)
end

#in_scope?(request_scope) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/rockoauth/model/authorization.rb', line 121

def in_scope?(request_scope)
  [*request_scope].all?(&scopes.method(:include?))
end

#scopesObject



125
126
127
128
# File 'lib/rockoauth/model/authorization.rb', line 125

def scopes
  scopes = scope ? scope.split(/\s+/) : []
  Set.new(scopes)
end