GithubHelp home page GithubHelp logo

ueberauth / ueberauth_identity Goto Github PK

View Code? Open in Web Editor NEW
79.0 7.0 21.0 46 KB

A username/password Strategy for Überauth

License: MIT License

Elixir 100.00%
ueberauth ueberauth-strategies identity strategy

ueberauth_identity's Introduction

Üeberauth Identity

Build Status Module Version Hex Docs Total Download License Last Updated

A simple username/password strategy for Überauth.

Installation

  1. Add :ueberauth_identity to your list of dependencies in mix.exs:

    def deps do
      [
        {:ueberauth_identity, "~> 0.3"}
      ]
    end
  2. Add the strategy to your applications:

    def application do
      [
        applications: [:ueberauth_identity]
      ]
    end
  3. Add Identity to your Überauth configuration:

    config :ueberauth, Ueberauth,
      providers: [
        identity: {Ueberauth.Strategy.Identity, [
          callback_methods: ["POST"]
        ]}
      ]
  4. Include the Überauth plug in your controller:

    defmodule MyApp.AuthController do
      use MyApp.Web, :controller
      plug Ueberauth
      ...
    end
  5. Create the request and callback routes if you haven't already:

    scope "/auth", MyApp do
      pipe_through :browser
    
      get "/:provider", AuthController, :request
      get "/:provider/callback", AuthController, :callback
      post "/identity/callback", AuthController, :identity_callback
    end
  6. Your request phase handler should implement a form or similar method to collect the required login information.

  7. The controller callback should validate login information using the Ueberauth.Auth struct:

    def identity_callback(%{assigns: %{ueberauth_auth: auth}} = conn, params) do
      case validate_password(auth.credentials) do
        :ok ->
          user = %{id: auth.uid, name: name_from_auth(auth), avatar: auth.info.image}
          conn
          |> put_flash(:info, "Successfully authenticated.")
          |> put_session(:current_user, user)
          |> redirect(to: "/")
        { :error, reason } ->
          conn
          |> put_flash(:error, reason)
          |> redirect(to: "/")
      end
    end

For an example implementation see the Überauth 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:

%{ "user" => { "email" => "[email protected]"}

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:

config :ueberauth, Ueberauth,
  providers: [
    identity: {Ueberauth.Strategy.Identity, [param_nesting: "user"]}
  ]

Params scrubbing

By default Überauth Identity will be changing empty values from the returned params to nil. If you want to disable that behaviour set the following option in your config:

config :ueberauth, Ueberauth,
  providers: [
    identity: {Ueberauth.Strategy.Identity, [scrub_params: false]}
  ]

Calling

Depending on the configured url you can initial the request through:

/auth/identity/callback

Copyright and License

Copyright (c) 2015 Daniel Neighman

Released under the MIT License, which can be found in the repository in LICENSE.

ueberauth_identity's People

Contributors

bklang avatar doomspork avatar imranismail avatar kianmeng avatar lessless avatar mbuhot avatar mikereinmiller avatar scrogson avatar stevedomin avatar xtian avatar ybur-yug avatar yordis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ueberauth_identity's Issues

How do you clear the password from the assigns?

When the user enter's an incorrect password, I want to re-render the login page with their email still filled but not their password. It seems like I may have to reach into the %Ueberauth.Auth{} struct in conn.assigns to accomplish this. Is that correct? Are there any methods to accomplish this? I don't see any in the docs.

Using Strategy.Identity: AuthView.render/2 is undefined (module MyApp.AuthView is not available)

Steps to Reproduce

Set as the example. Phoenix 1.13

Expected Result

Render the page "request.html.heex" when using Strategy.Identity

Actual Result

On signin with username/pwd, you send a request "GET /auth/identity", but even if "views/auth_view.ex" exists, I get an "AuthView.render/2 is undefined, and MyApp.AuthView is not available.
Request: GET /auth/identity
** (exit) an exception was raised:
** (UndefinedFunctionError) function MyApp.AuthView.render/2 is undefined (module MyApp.AuthView is not available)
MyApp.AuthView.render("request.html", %{callback_url: "http://localhost:4000/auth/identity/callback",...

Jsonapi & nested attributes

Hey there,

I'm currently building out an API that conforms to the Jsonapi.org specification. I noticed that we can account for a nested attribute like %{user => {email, password}, but in order to conform to the spec I need to be able to send attributes nested two levels deep, for example:

{
  "data": {
    "type": "users",
      "attributes": {
        "email": "[email protected]",
        "password": "secret"
      }
    }
}

I'd be happy to try to PR this, but I'm still pretty new to Elixir!

doc error, ( I think )

At the end of the README, in the "Calling" section

Depending on the configured url you can initial the request through:

/auth/identity

Shouldn't this be

/auth/identity/callback

In addition, shouldn't this just be shared with the real oauth callbacks and be function matched somewhere on %{provider: strategy} ? Otherwise, if its on its own, its not really a callback, just a controller method you are submitting to. ( The ueberauth example does what I'm suggesting )

Question: Persist in User table or Oauth authorization table?

Looking at an example from

https://github.com/hassox/phoenix_guardian

It seems that hassox, the author, is saving the password in the same sql table as the other Oauth authentications under the token field.

I know that ueberauth_identity doesn't care how you store or lookup a user from identity authentication but what do you guys recommend, should i store the user password in the user table in the password field or just along with the rest of the oauth authentications under the token field?

Hexdocs link is broken

I wasn't able to view the hexdocs for the package, it kept redirecting to https://hexdocs.pm/ueberauth_identity/extra-readme.html which results in a 404.

May be just a matter of replacing extra-readme with readme in the mix.exs file?

Issues with current elixir

There are a listing of warnings that will become errors later that need to be fixed:

warning: Dict.get/3 is deprecated, use the Map module for working with maps or the Keyword module for working with keyword lists
  lib/ueberauth/strategy/identity.ex:57

warning: Dict.get/2 is deprecated, use the Map module for working with maps or the Keyword module for working with keyword lists
  lib/ueberauth/strategy/identity.ex:57

warning: variable "default_options" does not exist and is being expanded to "default_options()", please use parentheses to remove the ambiguity or change the variable name
  lib/ueberauth/strategy/identity.ex:57

Can you customize credentials field?

How do I modify the credentials field? For example I won't need password confirmation in a login page and I would want the email there. Also under auth.info why are there so many fields? I wouldn't need half of them like phone location etc.

Compile warning on 0.2.3 on Elixir 1.8.0-otp-21.2.2

Latest released version is 0.2.3 still.

When depender project is compiling with warnings as errors then it fails to build:

warning: variable "package" does not exist and is being expanded to "package()", please use parentheses to remove the ambiguity or change the variable name
  /home/<user>/<project>/deps/ueberauth_identity/mix.exs:11

warning: variable "description" does not exist and is being expanded to "description()", please use parentheses to remove the ambiguity or change the variable name
  /home/<user>/<project>/deps/ueberauth_identity/mix.exs:17

warning: variable "deps" does not exist and is being expanded to "deps()", please use parentheses to remove the ambiguity or change the variable name
  /home/<user>/<project>/deps/ueberauth_identity/mix.exs:18

warning: variable "docs" does not exist and is being expanded to "docs()", please use parentheses to remove the ambiguity or change the variable name
  /home/<user>/<project>/deps/ueberauth_identity/mix.exs:19

warning: variable "docs_extras" does not exist and is being expanded to "docs_extras()", please use parentheses to remove the ambiguity or change the variable name
  /home/<user>/<project>/deps/ueberauth_identity/mix.exs:39

==> ueberauth_identity
Compiling 2 files (.ex)
warning: variable "default_options" does not exist and is being expanded to "default_options()", please use parentheses to remove the ambiguity or change the variable name
  lib/ueberauth/strategy/identity.ex:57

warning: Dict.get/2 is deprecated. Use the Map module for working with maps or the Keyword module for working with keyword lists
  lib/ueberauth/strategy/identity.ex:57

warning: Dict.get/3 is deprecated. Use the Map module for working with maps or the Keyword module for working with keyword lists
  lib/ueberauth/strategy/identity.ex:57

Generated ueberauth_identity app

Add identity management capabilities

Hi,

Do you think those functionalities can be merged in this strategy ?

Invitable: sends invites to new users with a sign-up link, allowing the user to create their account with their own password.
Registerable: allows anonymous users to register a users email address and password.
Confirmable: new accounts require clicking a link in a confirmation email.
Recoverable: provides a link to generate a password reset link with token expiry.
Trackable: saves login statistics like login counts, timestamps, and IP address for each user.
Lockable: locks an account when a specified number of failed sign-in attempts has been exceeded.
Unlockable With Token: provides a link to send yourself an unlock email.
Rememberable: provides persistent login with 'Remember me?' check box on login page.

Support for form_for field in config.exs

Issue

Params submitted from this form doesn't get captured into auth struct

# web/templates/auth/request

<%= form_for @changeset, @callback_url, fn f -> %>
  <%= if f.errors != [] do %>
    <div class="alert alert-danger">
      <p>Oops, something went wrong! Please check the errors below:</p>
      <ul>
        <%= for {attr, message} <- f.errors do %>
          <li><%= humanize(attr) %> <%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="form-group">
    <label>Email</label>
    <%= text_input f, :email, class: "form-control" %>
  </div>

  <div class="form-group">
    <label>Password</label>
    <%= password_input f, :password, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= submit "Login", class: "btn btn-primary" %>
  </div>
<% end %>

params

%{"_csrf_token" => "GXElfW8jABYYE15FAQoPdjcWKT4MNgAAw8W7ZQjLagluU267oTpRbQ==",
  "_utf8" => "✓", "provider" => "identity",
  "user" => %{"email" => "[email protected]", "password" => "password"}}
# config/config.exs
config :ueberauth, Ueberauth,
  providers: [
    identity: {Ueberauth.Strategy.Identity, [
      callback_methods: ["POST"],
      uid_field: :email,
      password_field: :password
    ]}
  ]

Suggestion

allow specifying model to be authenticated against in config so that it can be made useful to collect the fields from a form_for submission

config :ueberauth, Ueberauth,
  providers: [
    identity: {Ueberauth.Strategy.Identity, [
      callback_methods: ["POST"],
      uid_field: :email,
      password_field: :password,
      model: MyApp.User
    ]}
  ]

undefined function validate_password/1

I've tried following the docs for ueberauth_identity but getting:

** (CompileError) web/controllers/auth_controller.ex:32: undefined function validate_password/1

Error: https://travis-ci.org/dwyl/auth/builds/215054975#L391
Snapshot of code: https://github.com/dwyl/auth/blob/fe8f18a16695b5c2a4ca08c75a02bcd698c202b2/web/controllers/auth_controller.ex#L32

I searched through the ueberauth org:
https://github.com/search?q=org%3Aueberauth+validate_password&type=Code
image
There's only one place it's referred to and that's in the readme. (no code examples)

also checked all 14 answers on StackOverflow http://stackoverflow.com/search?q=ueberauth
(as instructed in your CONTRIBUTING.md but didn't find anything resembling this issue...)
image

There are quite a few questions that have no answers or comments ...
So I'm asking my question here in the hope of a response.

Is there an example definition for the validate_password/1 function?
or can anybody who has successfully implemented ueberauth_identity shed some light on this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.