-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from doorkeeper-gem/feat/nonce-tracking
Support nonces
- Loading branch information
Showing
32 changed files
with
532 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,6 @@ def prompt_values | |
end | ||
end | ||
end | ||
|
||
Helpers::Controller.send :prepend, OpenidConnect::Helpers::Controller | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Doorkeeper | ||
module OpenidConnect | ||
module OAuth | ||
module Authorization | ||
module Code | ||
def issue_token | ||
super.tap do |access_grant| | ||
::Doorkeeper::OpenidConnect::Nonce.create!( | ||
access_grant: access_grant, | ||
nonce: pre_auth.nonce | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
OAuth::Authorization::Code.send :prepend, OpenidConnect::OAuth::Authorization::Code | ||
end |
17 changes: 17 additions & 0 deletions
17
lib/doorkeeper/openid_connect/oauth/authorization_code_request.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,17 @@ | ||
module Doorkeeper | ||
module OpenidConnect | ||
module OAuth | ||
module AuthorizationCodeRequest | ||
private | ||
|
||
def after_successful_response | ||
super | ||
id_token = Doorkeeper::OpenidConnect::Models::IdToken.new(access_token, grant.openid_connect_nonce.use!) | ||
@response.id_token = id_token | ||
end | ||
end | ||
end | ||
end | ||
|
||
OAuth::AuthorizationCodeRequest.send :prepend, OpenidConnect::OAuth::AuthorizationCodeRequest | ||
end |
28 changes: 28 additions & 0 deletions
28
lib/doorkeeper/openid_connect/oauth/password_access_token_request.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,28 @@ | ||
module Doorkeeper | ||
module OpenidConnect | ||
module OAuth | ||
module PasswordAccessTokenRequest | ||
def self.prepended(base) | ||
base.class_eval do | ||
attr_reader :nonce | ||
end | ||
end | ||
|
||
def initialize(server, client, resource_owner, parameters = {}) | ||
super | ||
@nonce = parameters[:nonce] | ||
end | ||
|
||
private | ||
|
||
def after_successful_response | ||
super | ||
id_token = Doorkeeper::OpenidConnect::Models::IdToken.new(access_token, nonce) | ||
@response.id_token = id_token | ||
end | ||
end | ||
end | ||
end | ||
|
||
OAuth::PasswordAccessTokenRequest.send :prepend, OpenidConnect::OAuth::PasswordAccessTokenRequest | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Doorkeeper | ||
module OpenidConnect | ||
module OAuth | ||
module PreAuthorization | ||
def self.prepended(base) | ||
base.class_eval do | ||
attr_reader :nonce | ||
end | ||
end | ||
|
||
def initialize(server, client, attrs = {}) | ||
super | ||
@nonce = attrs[:nonce] | ||
end | ||
end | ||
end | ||
end | ||
|
||
OAuth::PreAuthorization.send :prepend, OpenidConnect::OAuth::PreAuthorization | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Doorkeeper | ||
module OpenidConnect | ||
module OAuth | ||
module TokenResponse | ||
def self.prepended(base) | ||
base.class_eval do | ||
attr_accessor :id_token | ||
end | ||
end | ||
|
||
def body | ||
if token.includes_scope? 'openid' | ||
super. | ||
merge({:id_token => id_token.try(:as_jws_token)}). | ||
reject { |_, value| value.blank? } | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
OAuth::TokenResponse.send :prepend, OpenidConnect::OAuth::TokenResponse | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Doorkeeper | ||
module OpenidConnect | ||
module Orm | ||
module ActiveRecord | ||
def initialize_models! | ||
super | ||
require 'doorkeeper/openid_connect/orm/active_record/access_grant' | ||
require 'doorkeeper/openid_connect/orm/active_record/nonce' | ||
|
||
if Doorkeeper.configuration.active_record_options[:establish_connection] | ||
[Doorkeeper::OpenidConnect::Nonce].each do |c| | ||
c.send :establish_connection, Doorkeeper.configuration.active_record_options[:establish_connection] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
Orm::ActiveRecord.singleton_class.send :prepend, OpenidConnect::Orm::ActiveRecord | ||
end |
16 changes: 16 additions & 0 deletions
16
lib/doorkeeper/openid_connect/orm/active_record/access_grant.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,16 @@ | ||
module Doorkeeper | ||
module OpenidConnect | ||
module AccessGrant | ||
def self.prepended(base) | ||
base.class_eval do | ||
has_one :openid_connect_nonce, | ||
class_name: 'Doorkeeper::OpenidConnect::Nonce', | ||
inverse_of: :access_grant, | ||
dependent: :delete | ||
end | ||
end | ||
end | ||
end | ||
|
||
AccessGrant.send :prepend, OpenidConnect::AccessGrant | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Doorkeeper | ||
module OpenidConnect | ||
class Nonce < ActiveRecord::Base | ||
self.table_name = "#{table_name_prefix}oauth_openid_connect_nonces#{table_name_suffix}".to_sym | ||
|
||
validates :access_grant_id, :nonce, presence: true | ||
belongs_to :access_grant, | ||
class_name: 'Doorkeeper::AccessGrant', | ||
inverse_of: :openid_connect_nonce | ||
|
||
def use! | ||
destroy! | ||
nonce | ||
end | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
lib/generators/doorkeeper/openid_connect/install_generator.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,11 @@ | ||
class Doorkeeper::OpenidConnect::InstallGenerator < ::Rails::Generators::Base | ||
include Rails::Generators::Migration | ||
source_root File.expand_path('../templates', __FILE__) | ||
desc 'Installs Doorkeeper OpenID Connect.' | ||
|
||
def install | ||
template 'initializer.rb', 'config/initializers/doorkeeper_openid_connect.rb' | ||
copy_file File.expand_path('../../../../../config/locales/en.yml', __FILE__), 'config/locales/doorkeeper_openid_connect.en.yml' | ||
route 'use_doorkeeper_openid_connect' | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
lib/generators/doorkeeper/openid_connect/migration_generator.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,15 @@ | ||
require 'rails/generators/active_record' | ||
|
||
class Doorkeeper::OpenidConnect::MigrationGenerator < ::Rails::Generators::Base | ||
include Rails::Generators::Migration | ||
source_root File.expand_path('../templates', __FILE__) | ||
desc 'Installs Doorkeeper OpenID Connect migration file.' | ||
|
||
def install | ||
migration_template 'migration.rb', 'db/migrate/create_doorkeeper_openid_connect_tables.rb' | ||
end | ||
|
||
def self.next_migration_number(dirname) | ||
ActiveRecord::Generators::Base.next_migration_number(dirname) | ||
end | ||
end |
41 changes: 41 additions & 0 deletions
41
lib/generators/doorkeeper/openid_connect/templates/initializer.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,41 @@ | ||
Doorkeeper::OpenidConnect.configure do | ||
|
||
issuer 'issuer string' | ||
|
||
jws_private_key <<-EOL | ||
-----BEGIN RSA PRIVATE KEY----- | ||
.... | ||
-----END RSA PRIVATE KEY----- | ||
EOL | ||
|
||
jws_public_key <<-EOL | ||
-----BEGIN RSA PUBLIC KEY----- | ||
.... | ||
-----END RSA PUBLIC KEY----- | ||
EOL | ||
|
||
resource_owner_from_access_token do |access_token| | ||
# Example implementation: | ||
# User.find_by(id: access_token.resource_owner_id) | ||
end | ||
|
||
subject do |resource_owner| | ||
# Example implementation: | ||
# resource_owner.key | ||
end | ||
|
||
# Expiration time on or after which the ID Token MUST NOT be accepted for processing. (default 120 seconds). | ||
# expiration 600 | ||
|
||
# Example claims: | ||
# claims do | ||
# normal_claim :_foo_ do |resource_owner| | ||
# resource_owner.foo | ||
# end | ||
|
||
# normal_claim :_bar_ do |resource_owner| | ||
# resource_owner.bar | ||
# end | ||
# end | ||
end | ||
|
Oops, something went wrong.