This repository was archived by the owner on Feb 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor to use oauth2 gem and proper OAuth patterns #11
Open
f3ndot
wants to merge
2
commits into
master
Choose a base branch
from
use-oauth2-gem
base: master
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,76 @@ | ||
| module Wealthsimple | ||
| def self.authorize_url(params = {}) | ||
| oauth_client.auth_code.authorize_url({ redirect_uri: config.redirect_uri }.merge(params)) | ||
| end | ||
|
|
||
| def self.get_token(token, redirect_uri = nil) | ||
| config.auth = oauth_client.auth_code.get_token(token, redirect_uri: redirect_uri || config.redirect_uri) | ||
| end | ||
|
|
||
| def self.get_application_token(params = {}) | ||
| access_token = oauth_client.client_credentials.get_token(params) | ||
| # abuse OAuth2 gem a bit, since we can't tell the context of how it was | ||
| # obtained and need to "refresh" client credentials automatically | ||
| access_token.instance_variable_get(:@params).merge!(_client_credentials: true, _params: params) | ||
| config.auth = access_token | ||
| end | ||
|
|
||
| def self.user_id | ||
| auth['resource_owner_id'] | ||
| end | ||
|
|
||
| def self.client_id | ||
| auth['client_canonical_id'] | ||
| end | ||
|
|
||
| def self.auth | ||
| raise AuthenticationError, 'Not authenticated' unless config.auth | ||
| if config.auth.expired? | ||
| return config.auth = config.auth.refresh! if config.auth.refresh_token | ||
| return get_application_token(config.auth[:_params]) if config.auth[:_client_credentials] | ||
| raise AuthenticationError, 'Access token expired' | ||
| end | ||
| config.auth | ||
| end | ||
|
|
||
| def self.auth=(auth_or_refresh_token) | ||
| case auth_or_refresh_token | ||
| when OAuth2::AccessToken | ||
| config.auth = auth_or_refresh_token | ||
| when Hash | ||
| config.auth = OAuth2::AccessToken.from_hash( | ||
| oauth_client, | ||
| auth_or_refresh_token | ||
| ) | ||
| when String | ||
| config.auth = OAuth2::AccessToken.from_hash( | ||
| oauth_client, | ||
| refresh_token: auth_or_refresh_token | ||
| ).refresh! | ||
| else | ||
| raise AuthenticationError, "Unknown auth value: #{auth_or_refresh_token}" | ||
| end | ||
| config.auth | ||
| end | ||
|
|
||
| def self.oauth_client | ||
| Wealthsimple.config.validate! | ||
| @oauth_client ||= OAuth2::Client.new( | ||
| Wealthsimple.config.client_id, | ||
| Wealthsimple.config.client_secret, | ||
| site: api_base_url, | ||
| authorize_url: "#{site_base_url}/authorize", | ||
| token_url: "#{api_base_url}/v1/oauth/token" | ||
| ) | ||
| end | ||
|
|
||
| def self.api_base_url | ||
| Wealthsimple.config.validate! | ||
| "https://api.#{Wealthsimple.config.env}.wealthsimple.com" | ||
| end | ||
|
|
||
| def self.site_base_url | ||
| Wealthsimple.config.validate! | ||
| Wealthsimple.config.env == 'production' ? 'https://my.wealthsimple.com' : 'https://staging.wealthsimple.com' | ||
| 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
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 |
|---|---|---|
| @@ -1,21 +1,26 @@ | ||
| require "bundler/setup" | ||
| require "wealthsimple" | ||
| require "pry" | ||
| require "dotenv/load" | ||
| require 'bundler/setup' | ||
| require 'wealthsimple' | ||
| require 'pry' | ||
| require 'dotenv/load' | ||
|
|
||
| Wealthsimple.configure do |config| | ||
| config.env = :sandbox | ||
| config.api_version = :v1 | ||
| config.client_id = "58a99e4862a1b246a7745523ca230e61dd7feff351056fcb22c73a5d7a2fcd69" | ||
| config.client_id = '58a99e4862a1b246a7745523ca230e61dd7feff351056fcb22c73a5d7a2fcd69' | ||
| config.client_secret = '3f3741cfad562a3d29d2b1835efed25e55d82f062fb4b53178e9679d9f248f20' | ||
| config.redirect_uri = 'urn:ietf:wg:oauth:2.0:oob' | ||
| end | ||
|
|
||
| auth = Wealthsimple.authenticate({ | ||
| "grant_type": "password", | ||
| "username": ENV["EMAIL"], | ||
| "password": ENV["PASSWORD"], | ||
| "scope": "read write", | ||
| }) | ||
| pp auth.resource | ||
| if ENV['AUTHORIZATION_CODE'] | ||
| Wealthsimple.get_token ENV['AUTHORIZATION_CODE'] | ||
| elsif ENV['REFRESH_TOKEN'] | ||
| Wealthsimple.auth = ENV['REFRESH_TOKEN'] | ||
| else | ||
| puts "Visit this URL in your browser:\n\n#{Wealthsimple.authorize_url}\n\n" | ||
| print 'What is the authorization code: ' | ||
| code = gets.strip | ||
| Wealthsimple.get_token code | ||
| end | ||
|
|
||
| user = Wealthsimple.get("/users/#{auth.resource.resource_owner_id}") | ||
| user = Wealthsimple.get("/users/#{Wealthsimple.user_id}") | ||
| pp user.resource |
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
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.
Is there no case where
client_secretwould be optional? (for ruby integrations)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.
Not unless they wanted to use Implicit or Password.
For Implicit it makes no sense to use as that's less privileged than Authorization Code.
For Password we want to steer people away from that and will likely remove it from our API offering