Class: Songkick::OAuth2::Model::Authorization
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Songkick::OAuth2::Model::Authorization
- Extended by:
- Hashing
- Defined in:
- lib/songkick/oauth2/model/authorization.rb
Class Method Summary collapse
- .create_access_token ⇒ Object
- .create_code(client) ⇒ Object
- .create_refresh_token(client) ⇒ Object
- .for(owner, client, attributes = {}) ⇒ Object
Instance Method Summary collapse
- #exchange! ⇒ Object
- #expired? ⇒ Boolean
- #expires_in ⇒ Object
- #generate_access_token ⇒ Object
- #generate_code ⇒ Object
- #grants_access?(user, *scopes) ⇒ Boolean
- #in_scope?(request_scope) ⇒ Boolean
- #scopes ⇒ Object
Methods included from Hashing
Class Method Details
.create_access_token ⇒ Object
35 36 37 38 39 40 |
# File 'lib/songkick/oauth2/model/authorization.rb', line 35 def self.create_access_token Songkick::OAuth2.generate_id do |token| hash = Songkick::OAuth2.hashify(token) count(:conditions => {:access_token_hash => hash}).zero? end end |
.create_code(client) ⇒ Object
29 30 31 32 33 |
# File 'lib/songkick/oauth2/model/authorization.rb', line 29 def self.create_code(client) Songkick::OAuth2.generate_id do |code| client..count(:conditions => {:code => code}).zero? end end |
.create_refresh_token(client) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/songkick/oauth2/model/authorization.rb', line 42 def self.create_refresh_token(client) Songkick::OAuth2.generate_id do |refresh_token| hash = Songkick::OAuth2.hashify(refresh_token) client..count(:conditions => {:refresh_token_hash => hash}).zero? end end |
.for(owner, client, attributes = {}) ⇒ Object
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 90 91 92 |
# File 'lib/songkick/oauth2/model/authorization.rb', line 49 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.(client) || new do || .owner = owner .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
94 95 96 97 98 99 |
# File 'lib/songkick/oauth2/model/authorization.rb', line 94 def exchange! self.code = nil self.access_token = self.class.create_access_token self.refresh_token = nil save! end |
#expired? ⇒ Boolean
101 102 103 104 |
# File 'lib/songkick/oauth2/model/authorization.rb', line 101 def expired? return false unless expires_at expires_at < Time.now end |
#expires_in ⇒ Object
106 107 108 |
# File 'lib/songkick/oauth2/model/authorization.rb', line 106 def expires_in expires_at && (expires_at - Time.now).ceil end |
#generate_access_token ⇒ Object
115 116 117 118 |
# File 'lib/songkick/oauth2/model/authorization.rb', line 115 def generate_access_token self.access_token ||= self.class.create_access_token save && access_token end |
#generate_code ⇒ Object
110 111 112 113 |
# File 'lib/songkick/oauth2/model/authorization.rb', line 110 def generate_code self.code ||= self.class.create_code(client) save && code end |
#grants_access?(user, *scopes) ⇒ Boolean
120 121 122 |
# File 'lib/songkick/oauth2/model/authorization.rb', line 120 def grants_access?(user, *scopes) not expired? and user == owner and in_scope?(scopes) end |
#in_scope?(request_scope) ⇒ Boolean
124 125 126 |
# File 'lib/songkick/oauth2/model/authorization.rb', line 124 def in_scope?(request_scope) [*request_scope].all?(&scopes.method(:include?)) end |
#scopes ⇒ Object
128 129 130 131 |
# File 'lib/songkick/oauth2/model/authorization.rb', line 128 def scopes scopes = scope ? scope.split(/\s+/) : [] Set.new(scopes) end |