forked from doorkeeper-gem/doorkeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'password-credentials-flow'
Fix conflicts with old FactoryGirl creation methods
- Loading branch information
Showing
19 changed files
with
435 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
# coding: utf-8 | ||
|
||
# TODO: refactor to DRY up, this is very similar to AccessTokenRequest | ||
module Doorkeeper::OAuth | ||
class PasswordAccessTokenRequest | ||
include Doorkeeper::Validations | ||
|
||
ATTRIBUTES = [ | ||
:client_id, | ||
:client_secret, | ||
:grant_type, | ||
:username, | ||
:password, | ||
:scope, | ||
:refresh_token | ||
] | ||
|
||
validate :attributes, :error => :invalid_request | ||
validate :grant_type, :error => :unsupported_grant_type | ||
validate :client, :error => :invalid_client | ||
validate :resource_owner, :error => :invalid_resource_owner | ||
validate :scope, :error => :invalid_scope | ||
|
||
attr_accessor *ATTRIBUTES | ||
attr_accessor :resource_owner | ||
|
||
def initialize(owner, attributes = {}) | ||
ATTRIBUTES.each { |attr| instance_variable_set("@#{attr}", attributes[attr]) } | ||
@resource_owner = owner | ||
@scope ||= Doorkeeper.configuration.default_scope_string | ||
validate | ||
end | ||
|
||
def authorize | ||
if valid? | ||
find_or_create_access_token | ||
end | ||
end | ||
|
||
def authorization | ||
auth = { | ||
'access_token' => access_token.token, | ||
'token_type' => access_token.token_type, | ||
'expires_in' => access_token.expires_in, | ||
} | ||
auth.merge!({'refresh_token' => access_token.refresh_token}) if refresh_token_enabled? | ||
auth | ||
end | ||
|
||
def valid? | ||
self.error.nil? | ||
end | ||
|
||
def access_token | ||
@access_token | ||
end | ||
|
||
def token_type | ||
"bearer" | ||
end | ||
|
||
def error_response | ||
{ | ||
'error' => error.to_s, | ||
'error_description' => error_description | ||
} | ||
end | ||
|
||
private | ||
|
||
def find_or_create_access_token | ||
if access_token | ||
access_token.expired? ? revoke_and_create_access_token : access_token | ||
else | ||
create_access_token | ||
end | ||
end | ||
|
||
def revoke_and_create_access_token | ||
access_token.revoke | ||
create_access_token | ||
end | ||
|
||
def revoke_base_token | ||
base_token.revoke | ||
end | ||
|
||
def client | ||
@client ||= Doorkeeper::Application.find_by_uid_and_secret(@client_id, @client_secret) | ||
end | ||
|
||
def create_access_token | ||
@access_token = Doorkeeper::AccessToken.create!({ | ||
:application_id => client.id, | ||
:resource_owner_id => resource_owner.id, | ||
:scopes => @scope, | ||
:expires_in => configuration.access_token_expires_in, | ||
:use_refresh_token => refresh_token_enabled? | ||
}) | ||
end | ||
|
||
def has_scope? | ||
Doorkeeper.configuration.scopes.all.present? | ||
end | ||
|
||
def validate_attributes | ||
grant_type.present? | ||
end | ||
|
||
def refresh_token_enabled? | ||
configuration.refresh_token_enabled? | ||
end | ||
|
||
def validate_client | ||
!!client | ||
end | ||
|
||
def validate_scope | ||
return true unless has_scope? | ||
ScopeChecker.valid?(scope, configuration.scopes) | ||
end | ||
|
||
def validate_grant_type | ||
grant_type == 'password' | ||
end | ||
|
||
def validate_resource_owner | ||
!!resource_owner | ||
end | ||
|
||
def error_description | ||
I18n.translate error, :scope => [:doorkeeper, :errors, :messages] | ||
end | ||
|
||
def configuration | ||
Doorkeeper.configuration | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
class User < ActiveRecord::Base | ||
has_secure_password | ||
validates_presence_of :password, :on => :create | ||
|
||
def self.authenticate!(name, password) | ||
owner = User.find_by_name(name) | ||
owner.authenticate(password) if owner | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
spec/dummy/db/migrate/20120312140401_add_password_to_users.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddPasswordToUsers < ActiveRecord::Migration | ||
def change | ||
add_column :users, :password_digest, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.