Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass parameters through an OAuth authentication so that my callback or controller can use?

I am using OAuth to a provider (LinkedIn).

I want to be able to pass parameters so that when someone signs-up, I can add some additional values at the time the new User is created (I am using Devise).

But how do I do that?

It looks like the link goes to the provider, which then makes a callback to my application. How can I pass parameters from that link?

like image 809
Satchel Avatar asked Apr 24 '11 22:04

Satchel


People also ask

How to use OAuth to connect to an API from console?

First, it is necessary to acquire OAuth 2.0 client credentials from API console. Then, the access token is requested from the authorization server by the client. It gets an access token from the response and sends the token to the API that you wish to access. You must send the user to the authorization endpoint at the beginning.

What does OAuth say about the user?

OAuth says absolutely nothing about the user, nor does it say how the user proved their presence or even if they're still there. As far as an OAuth client is concerned, it asked for a token, got a token, and eventually used that token to access some API.

How to obtain an OAuth2 access token?

OAuth 2.0 - Obtaining an Access Token 1 First, it is necessary to acquire OAuth 2.0 client credentials from API console. 2 Then, the access token is requested from the authorization server by the client. 3 It gets an access token from the response and sends the token to the API that you wish to access. More ...

What is OAuth 2 0 used for?

User Authentication with OAuth 2.0 The OAuth 2.0 specification defines a delegation protocol that is useful for conveying authorization decisions across a network of web-enabled applications and APIs. OAuth is used in a wide variety of applications, including providing mechanisms for user authentication.


2 Answers

If you add GET style params to the authentication url they will be available in the callback via the Rails request.env object under the omniauth.params key. For example

If you authenticate via:

link_to "Log In", "/auth/linkedin?foo=bar"

In the controller method mapped to GET /auth/:provider/callback you will have:

request.env['omniauth.params'] == { "foo" => "bar" }

Answer was a bit late, but I hope it helps someone.

like image 153
Matthew Avatar answered Oct 26 '22 14:10

Matthew


The easy way to do this is to set the parameters in the session, then access them in the callback.

In your action that redirects to the provider:

session[:additional] = additional_data_hash

In the action that handles the callback from the provider:

data = session.delete(:additional)

Use delete to ensure your session remains small for subsequent requests.

like image 44
Jonathan Julian Avatar answered Oct 26 '22 13:10

Jonathan Julian