This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
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 #7 from trineo/oauth
Oauth
- Loading branch information
Showing
18 changed files
with
405 additions
and
214 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ Gemfile.lock | |
pkg/* | ||
doc/* | ||
.rvmrc | ||
use-cases/ | ||
.idea* |
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 |
---|---|---|
|
@@ -22,3 +22,6 @@ | |
|
||
require 'vend/http_client' | ||
require 'vend/client' | ||
|
||
require 'vend/oauth2/client' | ||
require 'vend/oauth2/auth_code' |
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,48 @@ | ||
require 'oauth2' | ||
|
||
module Vend | ||
module Oauth2 | ||
|
||
class AuthCode | ||
|
||
DEFAULT_OPTIONS = {} | ||
AUTHORIZE_URL = '/connect' | ||
TOKEN_URL = '/api/1.0/token' | ||
|
||
attr_accessor :store, :client_id, :secret, :redirect_uri | ||
|
||
def initialize(store, client_id, secret, redirect_uri, options = {}) | ||
@store = store | ||
@client_id = client_id | ||
@secret = secret | ||
@redirect_uri = redirect_uri | ||
@options = DEFAULT_OPTIONS.merge(options) | ||
end | ||
|
||
def authorize_url | ||
get_oauth2_client.auth_code.authorize_url(:redirect_uri => redirect_uri) | ||
end | ||
|
||
def token_from_code(code) | ||
client = get_oauth2_client(store) | ||
client.auth_code.get_token(code, :redirect_uri => redirect_uri) | ||
end | ||
|
||
def refresh_token(auth_token, refresh_token) | ||
access_token = OAuth2::AccessToken.new(get_oauth2_client(store), auth_token, {refresh_token: refresh_token}) | ||
access_token.refresh! | ||
end | ||
|
||
protected | ||
def get_oauth2_client(domain_prefix = 'secure') | ||
OAuth2::Client.new(client_id, secret, { | ||
site: "https://#{domain_prefix}.vendhq.com", | ||
authorize_url: AUTHORIZE_URL, | ||
token_url: TOKEN_URL | ||
}) | ||
end | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Vend | ||
module Oauth2 | ||
|
||
class Client < Vend::Client | ||
|
||
DEFAULT_OPTIONS = {} | ||
|
||
include Logable | ||
|
||
attr_accessor :store, :auth_token | ||
|
||
def initialize(store, auth_token, options = {}) | ||
@store = store | ||
@auth_token = auth_token | ||
@options = DEFAULT_OPTIONS.merge(options) | ||
end | ||
|
||
def http_client | ||
@http_client ||= Vend::HttpClient.new(http_client_options) | ||
end | ||
|
||
def http_client_options | ||
options.merge( | ||
:auth_token => @auth_token, :base_url => base_url | ||
) | ||
end | ||
|
||
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
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
Oops, something went wrong.