]> git.openstreetmap.org Git - rails.git/blob - app/models/oauth2_verifier.rb
Defer deletion of avatars to a background job
[rails.git] / app / models / oauth2_verifier.rb
1 # == Schema Information
2 #
3 # Table name: oauth_tokens
4 #
5 #  id                    :integer          not null, primary key
6 #  user_id               :integer
7 #  type                  :string(20)
8 #  client_application_id :integer
9 #  token                 :string(50)
10 #  secret                :string(50)
11 #  authorized_at         :datetime
12 #  invalidated_at        :datetime
13 #  created_at            :datetime
14 #  updated_at            :datetime
15 #  allow_read_prefs      :boolean          default(FALSE), not null
16 #  allow_write_prefs     :boolean          default(FALSE), not null
17 #  allow_write_diary     :boolean          default(FALSE), not null
18 #  allow_write_api       :boolean          default(FALSE), not null
19 #  allow_read_gpx        :boolean          default(FALSE), not null
20 #  allow_write_gpx       :boolean          default(FALSE), not null
21 #  callback_url          :string
22 #  verifier              :string(20)
23 #  scope                 :string
24 #  valid_to              :datetime
25 #  allow_write_notes     :boolean          default(FALSE), not null
26 #
27 # Indexes
28 #
29 #  index_oauth_tokens_on_token    (token) UNIQUE
30 #  index_oauth_tokens_on_user_id  (user_id)
31 #
32 # Foreign Keys
33 #
34 #  oauth_tokens_client_application_id_fkey  (client_application_id => client_applications.id)
35 #  oauth_tokens_user_id_fkey                (user_id => users.id)
36 #
37
38 class Oauth2Verifier < OauthToken
39   validates :user, :presence => true, :associated => true
40
41   attr_accessor :state
42
43   def exchange!(_params = {})
44     OauthToken.transaction do
45       token = Oauth2Token.create! :user => user, :client_application => client_application, :scope => scope
46       invalidate!
47       token
48     end
49   end
50
51   def code
52     token
53   end
54
55   def redirect_url
56     callback_url
57   end
58
59   def to_query
60     q = "code=#{token}"
61     q << "&state=#{CGI.escape(state)}" if @state
62     q
63   end
64
65   protected
66
67   def generate_keys
68     self.token = OAuth::Helper.generate_key(20)[0, 20]
69     self.expires_at = 10.minutes.from_now
70     self.authorized_at = Time.now
71   end
72 end