Skip to content

Instantly share code, notes, and snippets.

@orendon
Last active March 11, 2019 15:06
Show Gist options
  • Save orendon/e6ce1f492e2f504c2e8a to your computer and use it in GitHub Desktop.
Save orendon/e6ce1f492e2f504c2e8a to your computer and use it in GitHub Desktop.
Doorkeeper Oauth2 - Authorization Flows
# https://github.com/doorkeeper-gem/doorkeeper/wiki/authorization-flow
resource_owner_authenticator do
current_user || redirect_to(new_user_session_url)
end
# ----
callback = "urn:ietf:wg:oauth:2.0:oob"
app_id = "28d7adbcf95a70fdcb6101a517645762b84a16ae5077b5e0b7a678f2f188b0d2"
secret = "0fc0c9ae38548404b001a3fdb3e9f4e90c22b78ba74df2104d7a37c1cb251687"
client = OAuth2::Client.new(app_id, secret, site: "http://localhost:3000/")
client.auth_code.authorize_url(redirect_uri: callback)
# curl client URL or access via browser in order to get authorization code
authorization_code = '9f3af1b0c3263f692ce8c8baabdaf0ea4d4d792fcc9d4a30439a79f5957872d5'
access = client.auth_code.get_token(authorization_code, redirect_uri: callback)
access.post("/api/v1/some/endpoint", params: { username: 'abc', password: 'xyz' })
# https://github.com/doorkeeper-gem/doorkeeper/wiki/Client-Credentials-flow
callback = "urn:ietf:wg:oauth:2.0:oob"
app_id = "28d7adbcf95a70fdcb6101a517645762b84a16ae5077b5e0b7a678f2f188b0d2"
secret = "0fc0c9ae38548404b001a3fdb3e9f4e90c22b78ba74df2104d7a37c1cb251687"
client = OAuth2::Client.new(app_id, secret, site: "http://localhost:3000/")
access = client.client_credentials.get_token
access.post("/api/v1/some/endpoint", params: { username: 'abc', password: 'xyz' })
# https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Resource-Owner-Password-Credentials-flow
Doorkeeper.configure do
resource_owner_from_credentials do |routes|
User.authenticate!(params[:username], params[:password])
end
end
# ----
callback = "urn:ietf:wg:oauth:2.0:oob"
app_id = "28d7adbcf95a70fdcb6101a517645762b84a16ae5077b5e0b7a678f2f188b0d2"
secret = "0fc0c9ae38548404b001a3fdb3e9f4e90c22b78ba74df2104d7a37c1cb251687"
client = OAuth2::Client.new(app_id, secret, site: "http://localhost:3000/")
access = client.password.get_token('user@example.com', 'sekretpwd')
access.post("/api/v1/some/endpoint", params: { foo: 'abc', bar: 'xyz' })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment