Last active
September 16, 2016 09:56
-
-
Save yudapc/f2d675a7ed4bc6724a3267e678daadfa to your computer and use it in GitHub Desktop.
OAuth2 with rails / ruby
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
location: `config/initializers/doorkeeper.rb`: | |
``` | |
resource_owner_authenticator do | |
current_user || begin | |
session[:user_return_to] = request.fullpath | |
redirect_to new_user_session_url | |
end | |
end | |
``` | |
if you want to skip authorize: | |
``` | |
skip_authorization do | |
true | |
end | |
``` | |
location `controllers`: | |
Paste this code under class | |
`before_action :doorkeeper_authorize!` | |
Check link, expected to get status code 401 | |
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
Testing oauth2 in ruby | |
------------------------------------------------ | |
$ gem install oauth2 | |
$ irb -r oauth2 | |
host_provider = 'http://localhost:3000' | |
callback = 'your callback link' | |
app_id = 'paste app id' | |
secret = 'paste secret' | |
client = OAuth2::Client.new(app_id, secret, site: host_provider) | |
#get code | |
client.auth_code.authorize_url(redirect_uri: callback) | |
#copy link and paste to browser, and then click allow/authorize | |
#paste code from query string | |
code = 'paste code from url' | |
access = client.auth_code.get_token(code, redirect_uri: callback) | |
#get access_token | |
access.token | |
#check token expired | |
access.expired? | |
#refresh token | |
access.refresh_token | |
new_access = access.refresh! | |
new_access.token | |
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
host_provider = 'http://localhost:3001' | |
callback = 'http://localhost:3002/auth/moka/callback' | |
app_id = 'df9b60aa6a4198da1bf216cbe2b5c9bb0edf0d2049e27ba970f787de33c04587' | |
secret = 'de113c590c9a3809b35b09665d6dd795b336a9094218561f3932e2f42194d8e6' | |
client = OAuth2::Client.new(app_id, secret, site: host_provider) | |
#get code | |
client.auth_code.authorize_url(redirect_uri: callback) | |
# GET access_token | |
curl -F grant_type=authorization_code \ | |
-F client_id=df9b60aa6a4198da1bf216cbe2b5c9bb0edf0d2049e27ba970f787de33c04587 \ | |
-F client_secret=de113c590c9a3809b35b09665d6dd795b336a9094218561f3932e2f42194d8e6 \ | |
-F code=957caa08d20e3de0546a2620bc43b4c4e1e1df449f1af0683f86813d4ddf2836 \ | |
-F redirect_uri=http://localhost:3002/auth/moka/callback \ | |
-X POST http://localhost:3001/oauth/token | |
#refresh token | |
curl -F grant_type=refresh_token \ | |
-F client_id=df9b60aa6a4198da1bf216cbe2b5c9bb0edf0d2049e27ba970f787de33c04587 \ | |
-F client_secret=de113c590c9a3809b35b09665d6dd795b336a9094218561f3932e2f42194d8e6 \ | |
-F refresh_token=6886d655d42c97193df2082e5f4b639e017810fd4025ddc5b5a6b6f00c05a941 \ | |
-X POST http://localhost:3001/oauth/token | |
#info token | |
curl -H "Authorization: Bearer bba8528f068839e0417459e18d112e78a4874ee9889201b0f331f709d5f21cad" \ | |
localhost:3001/oauth/token/info |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment