Class: Authorization

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Houston::Props
Defined in:
app/models/authorization.rb

Constant Summary

Constants included from Houston::Props

Houston::Props::VALID_PROP_NAME

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Houston::Props

#get_prop, #props, #update_prop!, #update_props!, valid_prop_name!, valid_prop_name?

Class Method Details

.for(user) ⇒ Object



16
17
18
# File 'app/models/authorization.rb', line 16

def for(user)
  where(user_id: user.id)
end

.grantedObject



20
21
22
# File 'app/models/authorization.rb', line 20

def granted
  where.not(access_token: nil)
end

.providerObject



35
36
37
# File 'app/models/authorization.rb', line 35

def provider
  @provider ||= Houston.oauth.get_provider(name.underscore)
end

.providersObject



31
32
33
# File 'app/models/authorization.rb', line 31

def providers
  Houston.config.oauth_providers.map(&:classify)
end

.set_access_token!(params) ⇒ Object



39
40
41
42
43
# File 'app/models/authorization.rb', line 39

def set_access_token!(params)
  Authorization.find(params.fetch(:state)).tap do |authorization|
    authorization.get_access_token! params.fetch(:code)
  end
end

.with_scope(*scopes) ⇒ Object Also known as: with_scopes



24
25
26
# File 'app/models/authorization.rb', line 24

def with_scope(*scopes)
  where("regexp_split_to_array(scope, '[,\\s]+') @> ARRAY[?]", scopes)
end

Instance Method Details

#access_tokenObject



68
69
70
71
# File 'app/models/authorization.rb', line 68

def access_token
  refresh! if expired?
  super
end

#authorize_url(params = {}) ⇒ Object



56
57
58
# File 'app/models/authorization.rb', line 56

def authorize_url(params={})
  provider.authorize_url params.merge(scope: scope, state: id)
end

#expired?Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'app/models/authorization.rb', line 73

def expired?
  return false if expires_in.nil?
  Time.now >= expires_at
end

#get_access_token!(code) ⇒ Object



64
65
66
# File 'app/models/authorization.rb', line 64

def get_access_token!(code)
  merge! provider.redeem_access_token(code)
end

#granted?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'app/models/authorization.rb', line 52

def granted?
  access_token.present?
end

#providerObject



48
49
50
# File 'app/models/authorization.rb', line 48

def provider
  self.class.provider
end

#refresh!Object



60
61
62
# File 'app/models/authorization.rb', line 60

def refresh!
  merge! provider.refresh_access_token(self)
end

#urlObject



78
79
80
# File 'app/models/authorization.rb', line 78

def url
  "#{Houston.root_url}/auth/#{id}"
end