~ubuntu-branches/debian/sid/ruby-omniauth-google-oauth2/sid

« back to all changes in this revision

Viewing changes to README.md

  • Committer: Package Import Robot
  • Author(s): Nitesh A Jain
  • Date: 2014-04-03 19:42:38 UTC
  • Revision ID: package-import@ubuntu.com-20140403194238-nsue6ukiup1rko17
Tags: upstream-0.2.2
ImportĀ upstreamĀ versionĀ 0.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# OmniAuth Google OAuth2 Strategy
 
2
 
 
3
Strategy to authenticate with Google via OAuth2 in OmniAuth.
 
4
 
 
5
Get your API key at: https://code.google.com/apis/console/
 
6
 
 
7
For more details, read the Google docs: https://developers.google.com/accounts/docs/OAuth2
 
8
 
 
9
## Installation
 
10
 
 
11
Add to your `Gemfile`:
 
12
 
 
13
```ruby
 
14
gem "omniauth-google-oauth2"
 
15
```
 
16
 
 
17
Then `bundle install`.
 
18
 
 
19
## Usage
 
20
 
 
21
Here's an example for adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
 
22
 
 
23
```ruby
 
24
Rails.application.config.middleware.use OmniAuth::Builder do
 
25
  provider :google_oauth2, ENV["GOOGLE_KEY"], ENV["GOOGLE_SECRET"]
 
26
end
 
27
```
 
28
 
 
29
You can now access the OmniAuth Google OAuth2 URL: `/auth/google_oauth2`
 
30
 
 
31
For more examples please check out `examples/omni_auth.rb`
 
32
 
 
33
## Configuration
 
34
 
 
35
You can configure several options, which you pass in to the `provider` method via a hash:
 
36
 
 
37
* `scope`: A comma-separated list of permissions you want to request from the user. See the [Google OAuth 2.0 Playground](https://developers.google.com/oauthplayground/) for a full list of available permissions. Caveats:
 
38
  * The `userinfo.email` and `userinfo.profile` scopes are used by default. By defining your own `scope`, you override these defaults. If you need these scopes, don't forget to add them yourself!
 
39
  * Scopes starting with `https://www.googleapis.com/auth/` do not need that prefix specified. So while you can use the smaller scope `books` since that permission starts with the mentioned prefix, you should use the full scope URL `https://docs.google.com/feeds/` to access a user's docs, for example.
 
40
* `prompt`: A space-delimited list of string values that determines whether the user is re-prompted for authentication and/or consent. Possible values are:
 
41
  * `none`: No authentication or consent pages will be displayed; it will return an error if the user is not already authenticated and has not pre-configured consent for the requested scopes. This can be used as a method to check for existing authentication and/or consent.
 
42
  * `consent`: The user will always be prompted for consent, even if he has previously allowed access a given set of scopes.
 
43
  * `select_account`: The user will always be prompted to select a user account. This allows a user who has multiple current account sessions to select one amongst them.
 
44
 
 
45
  If no value is specified, the user only sees the authentication page if he is not logged in and only sees the consent page the first time he authorizes a given set of scopes.
 
46
 
 
47
* `image_aspect_ratio`: The shape of the user's profile picture. Possible values are:
 
48
  * `original`: Picture maintains its original aspect ratio.
 
49
  * `square`: Picture presents equal width and height.
 
50
 
 
51
  Defaults to `original`.
 
52
 
 
53
* `image_size`: The size of the user's profile picture. The image returned will have width equal to the given value and variable height, according to the `image_aspect_ratio` chosen. Additionally, a picture with specific width and height can be requested by setting this option to a hash with `width` and `height` as keys. If only `width` or `height` is specified, a picture whose width or height is closest to the requested size and requested aspect ratio will be returned. Defaults to the original width and height of the picture.
 
54
 
 
55
* `name`: The name of the strategy. The default name is `google_oauth2` but it can be changed to any value, for example `google`. The OmniAuth URL will thus change to `/auth/google` and the `provider` key in the auth hash will then return `google`.
 
56
 
 
57
* `access_type`: Defaults to `offline`, so a refresh token is sent to be used when the user is not present at the browser. Can be set to `online`. Note that if you need a refresh token, google requires you to also to specify the option `prompt: 'consent'`, which is not a default.
 
58
 
 
59
Here's an example of a possible configuration where the strategy name is changed, the user is asked for extra permissions, the user is always prompted to select his account when logging in and the user's profile picture is returned as a thumbnail:
 
60
 
 
61
```ruby
 
62
Rails.application.config.middleware.use OmniAuth::Builder do
 
63
  provider :google_oauth2, ENV["GOOGLE_KEY"], ENV["GOOGLE_SECRET"],
 
64
    {
 
65
      :name => "google",
 
66
      :scope => "userinfo.email, userinfo.profile, plus.me, http://gdata.youtube.com",
 
67
      :prompt => "select_account",
 
68
      :image_aspect_ratio => "square",
 
69
      :image_size => 50
 
70
    }
 
71
end
 
72
```
 
73
 
 
74
## Auth Hash
 
75
 
 
76
Here's an example of an authentication hash available in the callback by accessing `request.env["omniauth.auth"]`:
 
77
 
 
78
```ruby
 
79
{
 
80
    :provider => "google_oauth2",
 
81
    :uid => "123456789",
 
82
    :info => {
 
83
        :name => "John Doe",
 
84
        :email => "john@company_name.com",
 
85
        :first_name => "John",
 
86
        :last_name => "Doe",
 
87
        :image => "https://lh3.googleusercontent.com/url/photo.jpg"
 
88
    },
 
89
    :credentials => {
 
90
        :token => "token",
 
91
        :refresh_token => "another_token",
 
92
        :expires_at => 1354920555,
 
93
        :expires => true
 
94
    },
 
95
    :extra => {
 
96
        :raw_info => {
 
97
            :id => "123456789",
 
98
            :email => "user@domain.example.com",
 
99
            :verified_email => true,
 
100
            :name => "John Doe",
 
101
            :given_name => "John",
 
102
            :family_name => "Doe",
 
103
            :link => "https://plus.google.com/123456789",
 
104
            :picture => "https://lh3.googleusercontent.com/url/photo.jpg",
 
105
            :gender => "male",
 
106
            :birthday => "0000-06-25",
 
107
            :locale => "en",
 
108
            :hd => "company_name.com"
 
109
        }
 
110
    }
 
111
}
 
112
```
 
113
 
 
114
### Devise
 
115
 
 
116
For devise, you should also make sure you have an OmniAuth callback controller setup
 
117
 
 
118
```ruby
 
119
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
 
120
  def google_oauth2
 
121
      # You need to implement the method below in your model (e.g. app/models/user.rb)
 
122
      @user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user)
 
123
 
 
124
      if @user.persisted?
 
125
        flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
 
126
        sign_in_and_redirect @user, :event => :authentication
 
127
      else
 
128
        session["devise.google_data"] = request.env["omniauth.auth"]
 
129
        redirect_to new_user_registration_url
 
130
      end
 
131
  end
 
132
end
 
133
```
 
134
 
 
135
and bind to or create the user
 
136
 
 
137
```ruby
 
138
def self.find_for_google_oauth2(access_token, signed_in_resource=nil)
 
139
    data = access_token.info
 
140
    user = User.where(:email => data["email"]).first
 
141
 
 
142
    unless user
 
143
        user = User.create(name: data["name"],
 
144
             email: data["email"],
 
145
             password: Devise.friendly_token[0,20]
 
146
            )
 
147
    end
 
148
    user
 
149
end
 
150
```
 
151
Detailed example at https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview#google-oauth2-example
 
152
 
 
153
## Build Status
 
154
[![Build Status](https://travis-ci.org/zquestz/omniauth-google-oauth2.png)](https://travis-ci.org/zquestz/omniauth-google-oauth2)
 
155
 
 
156
 
 
157
## License
 
158
 
 
159
Copyright (c) 2013 by Josh Ellithorpe
 
160
 
 
161
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 
162
 
 
163
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 
164
 
 
165
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.