-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OAuth 2.0 Server & Client Library. Both Bearer token type are supported. refs: https://github.com/nov/rack-oauth2
- Loading branch information
Showing
7 changed files
with
144 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Exit command with non-zero status code, Output logs of every command executed, Treat unset variables as an error when substituting. | ||
set -eou pipefail | ||
# Internal Field Separator - Linux shell variable | ||
IFS=$'\n\t' | ||
# Print shell input lines | ||
set -v | ||
|
||
# Set RBS_DIR variable to change directory to execute type checks using `steep check` | ||
RBS_DIR=$(cd $(dirname $0)/..; pwd) | ||
# Set REPO_DIR variable to validate RBS files added to the corresponding folder | ||
REPO_DIR=$(cd $(dirname $0)/../../..; pwd) | ||
# Validate RBS files, using the bundler environment present | ||
bundle exec rbs --repo $REPO_DIR -r rack-oauth2:2.2 validate --silent | ||
|
||
cd ${RBS_DIR}/_test | ||
# Run type checks | ||
bundle exec steep check | ||
|
||
$(git rev-parse --show-toplevel)/bin/check-untyped-call.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 @@ | ||
D = Steep::Diagnostic | ||
|
||
target :test do | ||
check "." | ||
signature "." | ||
|
||
repo_path "../../../" | ||
library "rack-oauth2" | ||
|
||
configure_code_diagnostics(D::Ruby.all_error) | ||
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,61 @@ | ||
# Write Ruby code to test the RBS. | ||
# It is type checked by `steep check` command. | ||
|
||
require "rack-oauth2" | ||
|
||
YOUR_CLIENT_ID = 'client_id' | ||
YOUR_CLIENT_SECRET = 'secret' | ||
YOUR_REDIRECT_URI = 'http://example.com' | ||
YOUR_USERNAME = 'username' | ||
YOUR_PASSWORD = 'password' | ||
YOUR_AUTHORIZATION_CODE = '1234' | ||
YOUR_REFRESH_TOKEN_FOR_USER = 'abcd' | ||
|
||
client = Rack::OAuth2::Client.new( | ||
:identifier => YOUR_CLIENT_ID, | ||
:secret => YOUR_CLIENT_SECRET, | ||
:redirect_uri => YOUR_REDIRECT_URI, # only required for grant_type = :code | ||
:host => 'rack-oauth2-sample.heroku.com' | ||
) | ||
|
||
request_type = :token | ||
|
||
puts "## request_type = :#{request_type}" | ||
|
||
authorization_uri = case request_type | ||
when :code | ||
client.authorization_uri(:scope => :user_about_me) | ||
when :token | ||
client.authorization_uri(:response_type => :token, :scope => :user_about_me) | ||
end | ||
|
||
puts authorization_uri | ||
|
||
|
||
client = Rack::OAuth2::Client.new( | ||
:identifier => YOUR_CLIENT_ID, | ||
:secret => YOUR_CLIENT_SECRET, | ||
:redirect_uri => YOUR_REDIRECT_URI, # only required for grant_type = :code | ||
:host => 'rack-oauth2-sample.heroku.com' | ||
) | ||
|
||
grant_type = :authorization_code | ||
|
||
puts "## grant_type = :#{grant_type}" | ||
|
||
case grant_type | ||
when :client_credentials | ||
# Nothing to do | ||
when :password | ||
client.resource_owner_credentials = YOUR_USERNAME, YOUR_PASSWORD | ||
when :authorization_code | ||
client.authorization_code = YOUR_AUTHORIZATION_CODE | ||
when :refresh_token | ||
client.refresh_token = YOUR_REFRESH_TOKEN_FOR_USER | ||
end | ||
|
||
begin | ||
p client.access_token! | ||
rescue => e | ||
p e | ||
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,8 @@ | ||
|
||
YOUR_CLIENT_ID: String | ||
YOUR_CLIENT_SECRET: String | ||
YOUR_REDIRECT_URI: String | ||
YOUR_USERNAME: String | ||
YOUR_PASSWORD: String | ||
YOUR_AUTHORIZATION_CODE: String | ||
YOUR_REFRESH_TOKEN_FOR_USER: String |
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,39 @@ | ||
module Rack | ||
module OAuth2 | ||
class AccessToken | ||
class Bearer < AccessToken | ||
end | ||
|
||
attr_accessor access_token: untyped | ||
attr_accessor token_type: untyped | ||
attr_accessor refresh_token: untyped | ||
attr_accessor expires_in: untyped | ||
attr_accessor scope: untyped | ||
end | ||
|
||
class Client | ||
class Error < StandardError | ||
attr_accessor status: Integer | ||
attr_accessor response: Hash[untyped, untyped] | ||
end | ||
|
||
class Grant | ||
class AuthorizationCode < Grant | ||
end | ||
|
||
class Password < Grant | ||
end | ||
|
||
class RefreshToken < Grant | ||
end | ||
end | ||
|
||
def initialize: (**untyped attributes) -> void | ||
def authorization_uri: (**untyped params) -> String | ||
def authorization_code=: (String code) -> Grant::AuthorizationCode | ||
def resource_owner_credentials=: ([String, String] credentials) -> Grant::Password | ||
def refresh_token=: (String token) -> Grant::RefreshToken | ||
def access_token!: () -> AccessToken::Bearer | ||
end | ||
end | ||
end |