Class: Opro::Oauth::AuthGrant
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Opro::Oauth::AuthGrant
- Defined in:
- app/models/opro/oauth/auth_grant.rb
Class Method Summary collapse
- .auth_with_code!(code, application_id) ⇒ Object
- .auth_with_user!(user, applicaiton_id, permissions = ::Opro.request_permissions) ⇒ Object
- .find_for_token(token) ⇒ Object
- .find_user_for_token(token) ⇒ Object
- .refresh_tokens!(refresh_token, application_id) ⇒ Object
Instance Method Summary collapse
- #can?(value) ⇒ Boolean
- #expired? ⇒ Boolean
- #expires_in ⇒ Object
- #generate_expires_at! ⇒ Object
- #generate_tokens! ⇒ Object
- #not_expired? ⇒ Boolean
- #redirect_uri_for(redirect_uri, state = nil) ⇒ Object
-
#unique_token_for(field, secure_token = SecureRandom.hex(16)) ⇒ Object
used to guarantee that we are generating unique codes, access_tokens and refresh_tokens.
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, = ::Opro.) return false unless user = .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 => ) 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
23 24 25 |
# File 'app/models/opro/oauth/auth_grant.rb', line 23 def can?(value) HashWithIndifferentAccess.new()[value] end |
#expired? ⇒ 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 < 0 end |
#expires_in ⇒ Object
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 |
# File 'app/models/opro/oauth/auth_grant.rb', line 82 def generate_tokens! self.code, self.access_token, self.refresh_token = unique_token_for(:refresh_token), unique_token_for(:access_token), unique_token_for(:refresh_token) end |
#not_expired? ⇒ 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
94 95 96 97 98 99 100 101 102 |
# File 'app/models/opro/oauth/auth_grant.rb', line 94 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
87 88 89 90 91 92 |
# File 'app/models/opro/oauth/auth_grant.rb', line 87 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 |