|
1 | 1 | # Überauth GitHub |
2 | 2 |
|
3 | | -Provides an Üeberauth strategy for authenticating with Github. |
| 3 | +> GitHub OAuth2 strategy for Überauth. |
4 | 4 |
|
5 | | -### Setup |
| 5 | +## Installation |
6 | 6 |
|
7 | | -Create an application in Github for you to use. |
| 7 | +1. Setup your application at [GitHub Developer](https://developer.github.com). |
8 | 8 |
|
9 | | -Register a new application at: [your github developer page](https://github.com/settings/developers) and get the `client_id` and `client_secret`. |
| 9 | +1. Add `:ueberauth_github` to your list of dependencies in `mix.exs`: |
10 | 10 |
|
11 | | -Include the provider in your configuration for Üeberauth |
| 11 | + ```elixir |
| 12 | + def deps do |
| 13 | + [{:ueberauth_github, "~> 0.1"}] |
| 14 | + end |
| 15 | + ``` |
12 | 16 |
|
13 | | -````elixir |
14 | | -config :ueberauth, Ueberauth, |
15 | | - providers: [ |
16 | | - github: [ { Ueberauth.Strategy.Github, [] } ] |
17 | | - ] |
18 | | -```` |
| 17 | +1. Add the strategy to your applications: |
19 | 18 |
|
20 | | -Then include the configuration for github. |
| 19 | + ```elixir |
| 20 | + def application do |
| 21 | + [applications: [:ueberauth_github]] |
| 22 | + end |
| 23 | + ``` |
21 | 24 |
|
22 | | -````elixir |
23 | | -config :ueberauth, Ueberauth.Strategy.Github.OAuth, |
24 | | - client_id: System.get_env("GITHUB_CLIENT_ID"), |
25 | | - client_secret: System.get_env("GITHUB_CLIENT_SECRET") |
26 | | -```` |
| 25 | +1. Add GitHub to your Überauth configuration: |
27 | 26 |
|
28 | | -If you haven't already, create a pipeline and setup routes for your callback handler |
| 27 | + ```elixir |
| 28 | + config :ueberauth, Ueberauth, |
| 29 | + providers: [ |
| 30 | + github: [{Ueberauth.Strategy.GitHub, []}] |
| 31 | + ] |
| 32 | + ``` |
29 | 33 |
|
30 | | -````elixir |
31 | | -pipeline :auth do |
32 | | - Ueberauth.plug "/auth" |
33 | | -end |
| 34 | +1. Update your provider configuration: |
34 | 35 |
|
35 | | -scope "/auth" do |
36 | | - pipe_through [:browser, :auth] |
| 36 | + ```elixir |
| 37 | + config :ueberauth, Ueberauth.Strategy.GitHub.OAuth, |
| 38 | + client_id: System.get_env("GITHUB_CLIENT_ID"), |
| 39 | + client_secret: System.get_env("GITHUB_CLIENT_SECRET") |
| 40 | + ``` |
37 | 41 |
|
38 | | - get "/:provider/callback", AuthController, :callback |
39 | | -end |
40 | | -```` |
| 42 | +1. Include the Überauth plug in your controller: |
41 | 43 |
|
| 44 | + ```elixir |
| 45 | + defmodule MyApp.AuthController do |
| 46 | + use MyApp.Web, :controller |
| 47 | + plug Ueberauth |
| 48 | + ... |
| 49 | + end |
| 50 | + ``` |
42 | 51 |
|
43 | | -Create an endpoint for the callback where you will handle the `Ueberauth.Auth` struct |
| 52 | +1. Create the request and callback routes if you haven't already: |
44 | 53 |
|
45 | | -````elixir |
46 | | -defmodule MyApp.AuthController do |
47 | | - use MyApp.Web, :controller |
| 54 | + ```elixir |
| 55 | + scope "/auth", MyApp do |
| 56 | + pipe_through :browser |
48 | 57 |
|
49 | | - def callback_phase(%{ assigns: %{ ueberauth_failure: fails } } = conn, _params) do |
50 | | - # do things with the failure |
51 | | - end |
| 58 | + get "/:provider", AuthController, :request |
| 59 | + get "/:provider/callback", AuthController, :callback |
| 60 | + end |
| 61 | + ``` |
52 | 62 |
|
53 | | - def callback_phase(%{ assigns: %{ ueberauth_auth: auth } } = conn, params) do |
54 | | - # do things with the auth |
55 | | - end |
56 | | -end |
57 | | -```` |
| 63 | +1. You controller needs to implement callbacks to deal with `Ueberauth.Auth` and `Ueberauth.Failure` responses. |
58 | 64 |
|
59 | | -You can edit the behaviour of the Strategy by including some options when you register your provider. |
| 65 | +For an example implementation see the [Überauth Example](https://github.com/ueberauth/ueberauth_example) application. |
60 | 66 |
|
61 | | -To set the `uid_field` |
| 67 | +## Calling |
62 | 68 |
|
63 | | -````elixir |
64 | | -config :ueberauth, Ueberauth, |
65 | | - providers: [ |
66 | | - github: [ { Ueberauth.Strategy.Github, [uid_field: :email] } ] |
67 | | - ] |
68 | | -```` |
| 69 | +Depending on the configured url you can initial the request through: |
| 70 | +
|
| 71 | + /auth/github |
69 | 72 |
|
70 | | -Default is `:login` |
| 73 | +Or with options: |
71 | 74 |
|
72 | | -To set the default 'scopes' (permissions): |
| 75 | + /auth/github?scope=user,public_repo |
73 | 76 |
|
74 | | -````elixir |
| 77 | +By default the requested scope is "user,public_repo". Scope can be configured either explicitly as a `scope` query value on the request path or in your configuration: |
| 78 | +
|
| 79 | +```elixir |
75 | 80 | config :ueberauth, Ueberauth, |
76 | 81 | providers: [ |
77 | | - github: [ { Ueberauth.Strategy.Github, [default_scope: "user,public_repo"] } ] |
| 82 | + github: {Ueberauth.Strategy.GitHub, [default_scope: "user,public_repo,notifications"]} |
78 | 83 | ] |
79 | | -```` |
80 | | - |
81 | | -Deafult is "user,public_repo" |
82 | | - |
83 | | -## Installation |
84 | | - |
85 | | -If [available in Hex](https://hex.pm/docs/publish), the package can be installed as: |
86 | | - |
87 | | - 1. Add ueber_github to your list of dependencies in `mix.exs`: |
88 | | - |
89 | | -````elixir |
90 | | -def deps do |
91 | | - [{:ueberauth_github, "~> 0.1.0"}] |
92 | | -end |
93 | | -```` |
94 | | - |
95 | | -# License |
96 | | - |
97 | | -The MIT License (MIT) |
98 | | - |
99 | | -Copyright (c) 2015 Daniel Neighman |
100 | | - |
101 | | -Permission is hereby granted, free of charge, to any person obtaining a copy |
102 | | -of this software and associated documentation files (the "Software"), to deal |
103 | | -in the Software without restriction, including without limitation the rights |
104 | | -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
105 | | -copies of the Software, and to permit persons to whom the Software is |
106 | | -furnished to do so, subject to the following conditions: |
| 84 | +``` |
107 | 85 |
|
108 | | -The above copyright notice and this permission notice shall be included in all |
109 | | -copies or substantial portions of the Software. |
| 86 | +## License |
110 | 87 |
|
111 | | -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
112 | | -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
113 | | -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
114 | | -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
115 | | -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
116 | | -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
117 | | -SOFTWARE. |
| 88 | +Please see [LICENSE](https://github.com/ueberauth/ueberauth_github/blob/master/LICENSE) for licensing details. |
0 commit comments