-
Notifications
You must be signed in to change notification settings - Fork 0
Introduce CORE Api Authentication #40
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
Open
imRohan
wants to merge
1
commit into
main
Choose a base branch
from
rl-core-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,68 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module IntelligentFoods | ||
| class V2 | ||
| attr_accessor :authentication | ||
| attr_reader :environment, :username, :password | ||
|
|
||
| PRODUCTION = "production" | ||
| STAGING = "staging" | ||
|
|
||
| def initialize(username: nil, password: nil, authentication: nil, | ||
| environment: STAGING) | ||
| @authentication = authentication | ||
| @environment = environment | ||
| @username = username | ||
| @password = password | ||
| end | ||
|
|
||
| def base_url | ||
| "https://core.intelligentfoods.#{tld}" | ||
| end | ||
|
|
||
| def authenticate! | ||
| return if authenticated? | ||
|
|
||
| authenticator = V2::Authenticator.build(self) | ||
| @authentication = authenticator.save! | ||
| end | ||
|
|
||
| def client | ||
| build_api_client! | ||
| end | ||
|
|
||
| def authenticated? | ||
| authentication.present? | ||
| end | ||
|
|
||
| def reset_authentication | ||
| @authentication = nil | ||
| self | ||
| end | ||
|
|
||
| def self.build(config: IntelligentFoods) | ||
| new(environment: config.environment, | ||
| username: config.username, | ||
| password: config.password) | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| def build_api_client! | ||
| authenticate! | ||
| IntelligentFoods::ApiClient.build(self) | ||
| end | ||
|
|
||
| def tld | ||
| @tld ||= determine_tld | ||
| end | ||
|
|
||
| def determine_tld | ||
| if environment == PRODUCTION | ||
| "com" | ||
| else | ||
| "dev" | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,44 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module IntelligentFoods | ||
| class V2::Authenticator | ||
| attr_accessor :base_url, :authentication | ||
|
|
||
| def initialize(base_url:, authentication:) | ||
| @base_url = base_url | ||
| @authentication = authentication | ||
| end | ||
|
|
||
| def save! | ||
| path = "#{base_url}/auth/info" | ||
| client = IntelligentFoods::ApiClient.new(authentication: authentication) | ||
| response = client.get(path: path) | ||
| handle_authentication_response!(response: response) | ||
| end | ||
|
|
||
| def self.build(api) | ||
| authentication = Authorization::Basic. | ||
| factory(client_id: api.username, | ||
| client_secret: api.password) | ||
| new(base_url: api.base_url, authentication: authentication) | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| def handle_authentication_response!(response:) | ||
| if response.success? | ||
| authentication | ||
| else | ||
| handle_errors! | ||
| end | ||
| end | ||
|
|
||
| def response_has_errors?(response) | ||
| response.has_key?(:error) | ||
| end | ||
|
|
||
| def handle_errors! | ||
| raise AuthenticationError.new(status: 401, title: "Authentication Failed") | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module IntelligentFoods | ||
| class CoreObject < IntelligentFoods::Object | ||
| def resources_path | ||
| "#{api.base_url}/api/#{object_name.downcase}" | ||
| end | ||
|
|
||
| def resource_path | ||
| "#{api.base_url}/api/#{object_name.downcase}/#{id}" | ||
| end | ||
|
|
||
| def api | ||
| @api ||= IntelligentFoods::V2.build | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,58 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe IntelligentFoods::V2::Authenticator do | ||
| describe "#save!" do | ||
| it "returns a authorization scheme" do | ||
| basic = IntelligentFoods::Authorization::Basic.new(token: "test") | ||
| auth = IntelligentFoods::V2::Authenticator. | ||
| new(base_url: "http://test.com", authentication: basic) | ||
| stub_authentication | ||
|
|
||
| result = auth.save! | ||
|
|
||
| expect(result).to respond_to(:header) | ||
| end | ||
|
|
||
| context "when authentication fails" do | ||
| it "raises a AuthenticationError" do | ||
| basic = IntelligentFoods::Authorization::Basic.new(token: "test") | ||
| auth = IntelligentFoods::V2::Authenticator. | ||
| new(base_url: "http://test.com", authentication: basic) | ||
| response_body = { error: "Authentication Failed" } | ||
| response = error_response(body: response_body) | ||
| stub_api_response response: response | ||
|
|
||
| expect { | ||
| auth.save! | ||
| }.to raise_error(IntelligentFoods::AuthenticationError) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe ".build" do | ||
| it "sets the base_url" do | ||
| api = IntelligentFoods::V2.new | ||
|
|
||
| result = IntelligentFoods::V2::Authenticator.build(api) | ||
|
|
||
| expect(result.base_url).to eq(api.base_url) | ||
| end | ||
|
|
||
| it "sets the authentication" do | ||
| api = IntelligentFoods::V2.new | ||
| basic = IntelligentFoods::Authorization::Basic.new(token: "test") | ||
| allow(IntelligentFoods::Authorization::Basic).to receive(:factory). | ||
| and_return(basic) | ||
|
|
||
| result = IntelligentFoods::V2::Authenticator.build(api) | ||
|
|
||
| expect(result.authentication).to eq(basic) | ||
| end | ||
| end | ||
|
|
||
| def build_config(environment: "staging", client_id: "cid_123", | ||
| client_secret: "cs_123") | ||
| OpenStruct.new(environment: environment, client_id: client_id, | ||
| client_secret: client_secret) | ||
| end | ||
| end |
This file contains hidden or 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,129 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe IntelligentFoods::V2 do | ||
| describe "#base_url" do | ||
| context "when the environment is production" do | ||
| it "returns the correct url" do | ||
| api = IntelligentFoods::V2.new(environment: "production") | ||
|
|
||
| result = api.base_url | ||
|
|
||
| expect(result).to eq("https://core.intelligentfoods.com") | ||
| end | ||
| end | ||
|
|
||
| context "when the environment is not production" do | ||
| it "returns the correct url" do | ||
| api = IntelligentFoods::V2.new(environment: "staging") | ||
|
|
||
| result = api.base_url | ||
|
|
||
| expect(result).to eq("https://core.intelligentfoods.dev") | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "#client" do | ||
| it "returns a api client" do | ||
| api = IntelligentFoods::V2.new | ||
| build_stubbed_authenticator(api) | ||
|
|
||
| result = api.client | ||
|
|
||
| expect(result).to respond_to(:post) | ||
| end | ||
| end | ||
|
|
||
| describe "#authenticate" do | ||
| it "builds a authenticator" do | ||
| api = IntelligentFoods::V2.new | ||
| build_stubbed_authenticator(api) | ||
|
|
||
| api.authenticate! | ||
|
|
||
| expect(IntelligentFoods::V2::Authenticator).to have_received(:build).once | ||
| end | ||
|
|
||
| it "authenticates" do | ||
| api = IntelligentFoods::V2.new | ||
| authenticator = build_stubbed_authenticator(api) | ||
|
|
||
| api.authenticate! | ||
|
|
||
| expect(authenticator).to have_received(:save!).once | ||
| end | ||
|
|
||
| context "when the authentication is present" do | ||
| it "is authenticated" do | ||
| api = IntelligentFoods::V2.new | ||
| build_stubbed_authenticator(api) | ||
|
|
||
| api.authenticate! | ||
|
|
||
| expect(api).to be_authenticated | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "reset authentication" do | ||
| it "removes the present authentication" do | ||
| auth = IntelligentFoods::Authorization::Basic.new(token: "1234") | ||
| api = IntelligentFoods::V2.new(authentication: auth) | ||
|
|
||
| result = api.reset_authentication | ||
|
|
||
| expect(result.authentication).to be_blank | ||
| end | ||
|
|
||
| it "is not authenticated" do | ||
| auth = IntelligentFoods::Authorization::Basic.new(token: "1234") | ||
| api = IntelligentFoods::V2.new(authentication: auth) | ||
|
|
||
| result = api.reset_authentication | ||
|
|
||
| expect(result).not_to be_authenticated | ||
| end | ||
| end | ||
|
|
||
| describe ".build" do | ||
| it "sets the environment" do | ||
| config = build_config(environment: "preview") | ||
|
|
||
| result = IntelligentFoods::V2.build(config: config) | ||
|
|
||
| expect(result.environment).to eq("preview") | ||
| end | ||
|
|
||
| it "sets the username" do | ||
| config = build_config(username: "secretUsername") | ||
|
|
||
| result = IntelligentFoods::V2.build(config: config) | ||
|
|
||
| expect(result.username).to eq("secretUsername") | ||
| end | ||
|
|
||
| it "sets the password" do | ||
| config = build_config(password: "secretPassword") | ||
|
|
||
| result = IntelligentFoods::V2.build(config: config) | ||
|
|
||
| expect(result.password).to eq("secretPassword") | ||
| end | ||
| end | ||
|
|
||
| def build_config(environment: "staging", username: "string", | ||
| password: "string") | ||
| OpenStruct.new(environment: environment, username: username, | ||
| password: password) | ||
| end | ||
|
|
||
| def build_stubbed_authenticator(api) | ||
| authenticator = IntelligentFoods::V2::Authenticator.build(api) | ||
| authentication = authenticator.authentication | ||
| client = IntelligentFoods::ApiClient.new(authentication: authentication) | ||
| allow(IntelligentFoods::V2::Authenticator).to receive(:build). | ||
| and_return(authenticator) | ||
| allow(authenticator).to receive(:save!).and_return(client) | ||
| authenticator | ||
| end | ||
| end |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why save? i don't think anything is actually being committed anywhere is it? Maybe
authenticate!is clearerThere 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.
This class has to have the same public interface as the authenticator for v1 which also uses
#save!(link). This decision was made initially to make the public api of the classes align with active record. Renaming would impact existing v1 classes, which if necessary should probably be done separately if we're now reconsidering naming.