Skip to content

Commit

Permalink
Merge pull request #6 from ueberauth/allow-param-nesting
Browse files Browse the repository at this point in the history
Allow nesting the params in the query
  • Loading branch information
Daniel Neighman committed Dec 4, 2015
2 parents 6fba341 + 78dd626 commit 3523538
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v 0.2.1

* Allow specifying a nested parameter to find the params in

# v 0.2.0

* Bump the version to follow the Ueberauth release.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@
For an example implementation see the [Überauth Example](https://github.com/ueberauth/ueberauth_example) application.
## Nested form attributes
Sometimes it's convenient to nest the returned params under a namespace. For
example if you're using a "user" form, your params may come back as:
```elixir
%{ "user" => { "email" => "my@email.com" … }
```
If you're using a nested set of attributes like this you'll need to let
Überauth Identity know about it. To do this set an option in your config:
```elixir
config :ueberauth, Ueberauth,
providers: [
identity: {Ueberauth.Strategy.Identity, [param_nesting: "user"]}
]
```
## Calling
Depending on the configured url you can initial the request through:
Expand Down
9 changes: 8 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@ use Mix.Config
config :ueberauth, Ueberauth,
providers: [
identity: { Ueberauth.Strategy.Identity, [] },
identity_with_options: { Ueberauth.Strategy.Identity, [uid_field: :username, nickname_field: :username] }
identity_with_options: {
Ueberauth.Strategy.Identity,
[
uid_field: :username,
nickname_field: :username,
param_nesting: "user"
]
}
]
16 changes: 14 additions & 2 deletions lib/ueberauth/strategy/identity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ defmodule Ueberauth.Strategy.Identity do
location_field: :location,
description_field: :description,
password_field: :password,
password_confirmation_field: :password_confirmation
password_confirmation_field: :password_confirmation,
param_nesting: nil

alias Ueberauth.Auth.Info
alias Ueberauth.Auth.Credentials
Expand Down Expand Up @@ -52,6 +53,17 @@ defmodule Ueberauth.Strategy.Identity do
end

defp param_for(conn, name) do
conn.params[to_string(option(conn, name))]
param_for(conn, name, option(conn, :param_nesting))
end

defp param_for(conn, name, nil) do
Map.get(conn.params, to_string(option(conn, name)))
end

defp param_for(conn, name, nesting) do
case Map.get(conn.params, to_string(nesting)) do
nil -> nil
nested -> Map.get(nested, to_string(option(conn, name)))
end
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule UeberauthIdentity.Mixfile do
[
{:ueberauth, "~> 0.2"},
{:plug, "~> 1.0"},
{:poison, "~> 1.5"},

# docs dependencies
{:earmark, "~> 0.1", only: :dev},
Expand Down
5 changes: 3 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
%{"earmark": {:hex, :earmark, "0.1.19"},
"ex_doc": {:hex, :ex_doc, "0.10.0"},
"plug": {:hex, :plug, "1.0.2"},
"ueberauth": {:hex, :ueberauth, "0.1.0"}}
"plug": {:hex, :plug, "1.0.3"},
"poison": {:hex, :poison, "1.5.0"},
"ueberauth": {:hex, :ueberauth, "0.2.0"}}
2 changes: 1 addition & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule SpecRouter do

plug :fetch_query_params

Ueberauth.plug "/auth"
plug Ueberauth, base_path: "/auth"

plug :match
plug :dispatch
Expand Down
39 changes: 20 additions & 19 deletions test/ueberauth/strategy/identity_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Ueberauth.Strategy.IdentityTest do
password: "sekrit",
password_confirmation: "sekrit"
}

query = URI.encode_query(opts)

conn = conn(:get, "/auth/identity/callback?#{query}") |> SpecRouter.call(@router)
Expand Down Expand Up @@ -63,16 +64,16 @@ defmodule Ueberauth.Strategy.IdentityTest do

test "overwridden callback phase" do
opts = %{
email: "foo@example.com",
name: "Fred Flintstone",
first_name: "Fred",
last_name: "Flintstone",
username: "freddy",
phone: "555-555-5555",
description: "Cave man",
location: "Bedrock",
password: "sekrit",
password_confirmation: "sekrit"
"user[email]" => "foo@example.com",
"user[name]" => "Fred Flintstone",
"user[first_name]" => "Fred",
"user[last_name]" => "Flintstone",
"user[username]" => "freddy",
"user[phone]" => "555-555-5555",
"user[description]" => "Cave man",
"user[location]" => "Bedrock",
"user[password]" => "sekrit",
"user[password_confirmation]" => "sekrit"
}
query = URI.encode_query(opts)

Expand All @@ -84,16 +85,16 @@ defmodule Ueberauth.Strategy.IdentityTest do

assert auth.provider == :identity_with_options
assert auth.strategy == Ueberauth.Strategy.Identity
assert auth.uid == opts.username
assert auth.uid == Map.get(opts, "user[username]")

info = auth.info
assert info.email == opts.email
assert info.name == opts.name
assert info.first_name == opts.first_name
assert info.last_name == opts.last_name
assert info.nickname == opts.username
assert info.phone == opts.phone
assert info.location == opts.location
assert info.description == opts.description
assert info.email == Map.get(opts, "user[email]")
assert info.name == Map.get(opts, "user[name]")
assert info.first_name == Map.get(opts, "user[first_name]")
assert info.last_name == Map.get(opts, "user[last_name]")
assert info.nickname == Map.get(opts, "user[username]")
assert info.phone == Map.get(opts, "user[phone]")
assert info.location == Map.get(opts, "user[location]")
assert info.description == Map.get(opts, "user[description]")
end
end

0 comments on commit 3523538

Please sign in to comment.