]> git.openstreetmap.org Git - rails.git/blobdiff - app/models/oauth2_verifier.rb
Make OAuth work again
[rails.git] / app / models / oauth2_verifier.rb
diff --git a/app/models/oauth2_verifier.rb b/app/models/oauth2_verifier.rb
new file mode 100644 (file)
index 0000000..94856d0
--- /dev/null
@@ -0,0 +1,34 @@
+class Oauth2Verifier < OauthToken
+  validates_presence_of :user
+  attr_accessor :state
+
+  def exchange!(params={})
+    OauthToken.transaction do
+      token = Oauth2Token.create! :user=>user,:client_application=>client_application, :scope => scope
+      invalidate!
+      token
+    end
+  end
+
+  def code
+    token
+  end
+
+  def redirect_url
+    callback_url
+  end
+
+  def to_query
+    q = "code=#{token}"
+    q << "&state=#{URI.escape(state)}" if @state
+    q
+  end
+
+  protected
+
+  def generate_keys
+    self.token = OAuth::Helper.generate_key(20)[0,20]
+    self.expires_at = 10.minutes.from_now
+    self.authorized_at = Time.now
+  end
+end