-
Notifications
You must be signed in to change notification settings - Fork 45
Allow controller level authentication #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
941cf68
82155a8
45bda76
2155f5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module GraphqlDevise | ||
| module Concerns | ||
| module AdditionalControllerMethods | ||
| extend ActiveSupport::Concern | ||
|
|
||
| included do | ||
| attr_accessor :client_id, :token, :resource | ||
| end | ||
|
|
||
| def gql_devise_context(models) | ||
| { | ||
| current_resource: authenticate_model(models), | ||
| controller: self | ||
| } | ||
| end | ||
|
|
||
| def authenticate_model(models) | ||
| Array(models).each do |model| | ||
| set_resource_by_token(model) | ||
| return @resource if @resource.present? | ||
| end | ||
|
|
||
| nil | ||
| end | ||
|
|
||
| def resource_class(resource = nil) | ||
| # Return the resource class instead of looking for a Devise mapping if resource is already a resource class | ||
| return resource if resource.respond_to?(:find_by) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In which cases will this not be an AR or Mongo Id model? |
||
|
|
||
| super | ||
| end | ||
|
|
||
| def full_url_without_params | ||
| request.base_url + request.path | ||
| end | ||
|
|
||
| def set_resource_by_token(resource) | ||
| set_user_by_token(resource) | ||
| end | ||
|
|
||
| def graphql_context(resource_name) | ||
| { | ||
| resource_name: resource_name, | ||
| controller: self | ||
| } | ||
| end | ||
|
|
||
| def build_redirect_headers(access_token, client, redirect_header_options = {}) | ||
| { | ||
| DeviseTokenAuth.headers_names[:"access-token"] => access_token, | ||
| DeviseTokenAuth.headers_names[:client] => client, | ||
| :config => params[:config], | ||
| :client_id => client, | ||
| :token => access_token | ||
| }.merge(redirect_header_options) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'graphql_devise/model/with_email_updater' | ||
|
|
||
| module GraphqlDevise | ||
| module Concerns | ||
| module AdditionalModelMethods | ||
| extend ActiveSupport::Concern | ||
|
|
||
| class_methods do | ||
| def reconfirmable | ||
| devise_modules.include?(:confirmable) && column_names.include?('unconfirmed_email') | ||
| end | ||
| end | ||
|
|
||
| def update_with_email(attributes = {}) | ||
| GraphqlDevise::Model::WithEmailUpdater.new(self, attributes).call | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,22 +20,36 @@ def self.load_schema | |
| @schema_loaded = true | ||
| end | ||
|
|
||
| def self.resource_mounted?(mapping_name) | ||
| @mounted_resources.include?(mapping_name) | ||
| def self.resource_mounted?(model) | ||
| @mounted_resources.include?(model) | ||
| end | ||
|
|
||
| def self.mount_resource(mapping_name) | ||
| @mounted_resources << mapping_name | ||
| def self.mount_resource(model) | ||
| @mounted_resources << model | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check the resource is not already there before adding?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we do on the resource_loader
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would less error prone if the check is closer to the assignation, like with
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With |
||
| end | ||
|
|
||
| def self.add_mapping(mapping_name, resource) | ||
| return if Devise.mappings.key?(mapping_name) | ||
| return if Devise.mappings.key?(mapping_name.to_sym) | ||
|
|
||
| Devise.add_mapping( | ||
| mapping_name.to_s.pluralize.to_sym, | ||
| module: :devise, class_name: resource | ||
| ) | ||
| end | ||
|
|
||
| def self.to_mapping_name(resource) | ||
| resource.to_s.underscore.tr('/', '_') | ||
| end | ||
|
|
||
| def self.configure_warden_serializer_for_model(model) | ||
| Devise.warden_config.serialize_into_session(to_mapping_name(model)) do |record| | ||
| model.serialize_into_session(record) | ||
| end | ||
|
|
||
| Devise.warden_config.serialize_from_session(to_mapping_name(model)) do |args| | ||
| model.serialize_from_session(*args) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| require 'graphql_devise/engine' | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this comment should mention
find_byis meant to use duck typing to indentify AR and Mongo ID models.