• Stars
    star
    148
  • Rank 249,983 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 14 years ago
  • Updated about 13 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Instant Oauth and OpenID support for your Rails and Sinatra Apps

AuthlogicConnect

Instant Oauth and OpenID support for your Rails and Sinatra Apps

AuthlogicConnect is an extension of the Authlogic library that adds complete Oauth and OpenID support to your application. It provides a single interface to Oauth 1.0 and Oauth 2.0.

It currently allows you to login with Github, Facebook, Twitter, Google, LinkedIn, MySpace, Vimeo, and Yahoo Oauth providers, and all the OpenID providers. Feel free to add support for more as you need them.

Here's a live example on Heroku (with source). Here's the Docs

Lists of known providers:

Install

1. Install AuthlogicConnect

sudo gem install authlogic-connect

2. Add the gem dependencies in your config:

Rails 2.3.x: config/environment.rb

config.gem "json"
config.gem "authlogic"
config.gem "oauth"
config.gem "oauth2"
config.gem "authlogic-connect"

Rails 3: Gemfile

gem "ruby-openid"
gem "rack-openid", ">=0.2.1", :require => "rack/openid"
gem "authlogic", :git => "git://github.com/odorcicd/authlogic.git", :branch => "rails3"
gem "oauth"
gem "oauth2"
gem "authlogic-connect"

3. Add the OpenIdAuthentication.store

Do to some strange problem I have yet to really understand, Rails 2.3.5 doesn't like when OpenIdAuthentication.store is null, which means it uses the "in memory" store and for some reason fails.

So as a fix, if you are using Rails < 3, add these at the end of your config/environment.rb files:

In development mode:

OpenIdAuthentication.store = :file

In production (on Heroku primarily)

OpenIdAuthentication.store = :memcache

4. Add the Migrations

See the Rails 2 Example and Rails 3 Example projects to see what you need. Will add a generator sometime.

Files needed are:

  • models: User, UserSession
  • controllers: UsersController, UserSessionsController, ApplicationController
  • migrations: create_users, create_sessions, create_tokens
  • initializers: config/authlogic.example.yml, config/initializers/authlogic_connect_config.rb
  • routes

5. Configure your keys

In config/authlogic.yml, write your keys and secrets for each service you would like to support. You have to manually go to the websites and register with the service provider (list of those links coming soon, in token classes for now).

connect:
  twitter:
    key: "my_key"
    secret: "my_secret"
    label: "Twitter"
  facebook:
    key: "my_key"
    secret: "my_secret"
    label: "Facebook"
  google:
    key: "my_key"
    secret: "my_secret"
    label: "Google"
  yahoo:
    key: "my_key"
    secret: "my_secret"
    label: "Yahoo"
  myspace:
    key: "my_key"
    secret: "my_secret"
  vimeo:
    key: "my_key"
    secret: "my_secret"
  linked_in:
    key: "my_key"
    secret: "my_secret"

These are then loaded via the initializer script in config/initializers/authlogic_connect_config.rb:

AuthlogicConnect.config = YAML.load_file("config/authlogic.yml")

6. Make sure you save your objects properly

Because of the redirects involved in Oauth and OpenID, you MUST pass a block to the save method in your UsersController and UserSessionsController:

@user_session.save do |result|
  if result
    flash[:notice] # "Login successful!"
    redirect_back_or_default account_url
  else
    render :action => :new
  end
end

If you don't use the block, we will get a DoubleRender error. We need the block to jump out of the rendering while redirecting.

Also, be sure to skip protect_from_forgery for actions using this. Even if logs say a GET request is issued, a POST route will need to bypass forgery protection in order to yield a result to the #save block when back from auth provider.

7. Add Parameters to Forms in your Views

There are 3 things to include in your views.

First, you must specify whether this is for registration or login. This is stored in the authentication_type key with a value of user for registration and session for login:

%input{:type => :hidden, :name => :authentication_type, :value => :user}

Second, if you are using Oauth, you must include an input with name oauth_provider and value twitter or whatever other provider you might want (see example apps for dynamic example).

%input{:type => :radio, :id => :twitter_oauth_provider, :name => :oauth_provider, :value => :twitter}

Finally, if you are using OpenID, you must include an input with name openid_identifier, which is a text field with the value the user types in for their address:

%input.nice{:type => :text, :name => :openid_identifier}

Those are passed as parameters to Authlogic, and the complicated details are abstracted away.

Overview of the User Experience

There are 3 ways you a user can login with AuthlogicConnect:

  1. Clicking an Oauth Provider
  2. Clicking an OpenID Provider and entering in their username
  3. Manually typing in a full OpenID address

Oauth is very different from OpenID, but this aims to make them work the same.

Examples

These are examples of what you can get from a User. Code is placed in controller for demo purposes, it should be abstracted into the model.

API

User model has the following public accessors and methods. This example assumes:

  • You've associated your Google, OpenID, and Twitter accounts with this app.
  • You're currently logged in via Google.

Inside the show method in a controller...

def show
  @user = @current_user
  
  puts @user.tokens #=> [
    #<OpenidToken id: 12, user_id: 9, type: "OpenidToken", key: "http://my-openid-login.myopenid.com/", token: nil, secret: nil, active: nil, created_at: "2010-05-24 14:52:19", updated_at: "2010-05-24 14:52:19">,
    #<TwitterToken id: 13, user_id: 9, type: "TwitterToken", key: "my-twitter-id-123", token: "twitter-token", secret: "twitter-secret", active: nil, created_at: "2010-05-24 15:03:05", updated_at: "2010-05-24 15:03:05">,
    #<GoogleToken id: 14, user_id: 9, type: "GoogleToken", key: "[email protected]", token: "google-token", secret: "google-secret", active: nil, created_at: "2010-05-24 15:09:04", updated_at: "2010-05-24 15:09:04">]
      
  puts @user.tokens.length #=> 3
  
  # currently logged in with...
  puts @user.active_token #=> #<GoogleToken id: 14, user_id: 9, type: "GoogleToken", key: "[email protected]", token: "google-token", secret: "google-secret", active: nil, created_at: "2010-05-24 15:09:04", updated_at: "2010-05-24 15:09:04">
    
  puts @user.authenticated_with #=> ["twitter", "openid", "google"]
  puts @user.authenticated_with?(:twitter) #=> true
  puts @user.authenticated_with?(:facebook) #=> false
  
  puts @user.has_token?(:google) #=> true
  
  puts @user.get_token(:google) #=> #<GoogleToken id: 14, user_id: 9, type: "GoogleToken", key: "[email protected]", token: "google-token", secret: "google-secret", active: nil, created_at: "2010-05-24 15:09:04", updated_at: "2010-05-24 15:09:04">
  
  # change active_token
  @user.active_token = @user.get_token(:twitter)
  puts @user.active_token #=> #<TwitterToken id: 13, user_id: 9, type: "TwitterToken", key: "my-twitter-id-123", token: "twitter-token", secret: "twitter-secret", active: nil, created_at: "2010-05-24 15:03:05", updated_at: "2010-05-24 15:03:05">
  
  # access oauth api
  @twitter = @user.active_token
  @twitter_profile = JSON.parse(@twitter.get("/account/verify_credentials.json").body) #=> twitter api stuff
  # ...
end

Get Facebook Data

If they've associated their Facebook account with your site, you can access Facebook data.

def show
  @user = @current_user
  token = @user.active_token # assuming this is FacebookToken
  facebook = JSON.parse(token.get("/me"))
  @profile = {
    :id     => facebook["id"],
    :name   => facebook["name"],
    :photo  => "https://graph.facebook.com/#{facebook["id"]}/picture",
    :link   => facebook["link"],
    :title  => "Facebook"
  }
  @profile = @user.profile
end

Helpful links

Rest...

Thanks for the people that are already extending the project, all the input making things move much faster. Andrew Cove and Daf have helped me quite a bit, thanks guys.

Feel free to add to the wiki if you figure things out or make new distinctions.

Flow

  • Try to create a session
  • Session logs into provider
  • On success, if no user, redirect to User#create

Notes

  • Build mechanize tool to automatically create applications with service providers.

Todo

  • Add Andrew Cove's idea of a "Merge Code". So if user creates Facebook account logs out, and create Twitter account, a code they can use to pass to facebook account so it knows it's associated with Twitter.

More Repositories

1

cached-commons

Caching and Compressing JavaScripts and Stylesheets old school
JavaScript
115
star
2

mint.js

NO LONGER SUPPORTED
CoffeeScript
102
star
3

tiny-require.js

Tiny require library for the browser. 3.0kb minified. 1.2kb gzipped.
CoffeeScript
59
star
4

queuable

Free Cron and Background Jobs for Heroku
Python
55
star
5

authlogic-connect-example

Authlogic, OpenID, Oauth, Rails 3, on Heroku
JavaScript
34
star
6

cockpit

Super DRY Settings for Ruby, Rails, and Sinatra Apps
Ruby
23
star
7

authlogic-connect-example-rails2

Let your app use all of Oauth and OpenID (Rails 2)
JavaScript
15
star
8

mayan-hieroglyphs

1000+ Free Mayan Hieroglyph Icons
13
star
9

ubiquitously

Social Publishing gem
Ruby
8
star
10

underscore.logger

Cross-browser and Node.js empowered logging with colorized output.
CoffeeScript
7
star
11

cropify

JavaScript
6
star
12

timezones

A human-friendly timezone converter in JavaScript
JavaScript
6
star
13

seesaw

JavaScript
4
star
14

open-window

JavaScript
3
star
15

make-link-text.js

Basic Link Text Parser Prototype
JavaScript
3
star
16

cpp2ts.js

C++ to TypeScript Converter (to a Rough Approximationâ„¢)
C++
3
star
17

tierra.aws.cloud.js

Experiments with Multi-Region Infrastructure with Terraform on AWS
HCL
2
star
18

tierra.js

Terraform Script Builder in JavaScript
JavaScript
2
star
19

awesome-inspiration

These are all projects to gain inspiration from for implementation, in various programming languages.
2
star
20

computer-science

Papers and Notes on Ideal Compiler Architecture Design
HTML
2
star
21

vm.js

Minimal Virtual Machine Demo in JavaScript
JavaScript
2
star
22

text

Public Domain Text Collections
Shell
2
star
23

script-tree

JavaScript
2
star
24

meditations

Meditations on Swift, Assembly, and JavaScript
2
star
25

quadratic-residue-prng.js

Quadratic Residue Permutation Oriented Pseudo Random Number Generator in JavaScript
JavaScript
2
star
26

print

Print HTML elements on a printer.
JavaScript
2
star
27

normalize-ast.js

Normalize a JavaScript AST in a special way
JavaScript
2
star
28

lancejpollard

Lance's README
1
star
29

bind.js

A binding library to make react forms more performant
JavaScript
1
star
30

base-make-javascript-code.js

JavaScript
1
star
31

ht.js

Hyperbolic tessellations in JavaScript.
TypeScript
1
star
32

configured-quadratic-residue-prng.js

Configured Quadratic Residue Permutation Oriented Pseudo Random Number Generator in JavaScript
JavaScript
1
star
33

next-2-repos

Bug demonstrating FOUC in next.js
JavaScript
1
star
34

gematria.js

Cross-Cultural Gematria in JavaScript
TypeScript
1
star
35

js2tree.js

Convert JavaScript to Link Text
JavaScript
1
star
36

clear-diacritics.js

Clear diacritics to make text more suitable for a URL string
JavaScript
1
star
37

bit.js

Bit Manipulation Experiments
JavaScript
1
star
38

hash

Hashing Function Theory
JavaScript
1
star
39

api.json

API docs in JSON form
1
star
40

programming-language-cheat-sheet

How to do stuff in a programming language with as little learning as possible
1
star
41

functions.js

Collection of functions
JavaScript
1
star
42

language-detector.js

Detect what kind of language some text might be.
JavaScript
1
star
43

awesome-api

Curated List of APIs, because most of the API lists out there are junk
1
star
44

math-notation

Math Notation Collection
Jupyter Notebook
1
star
45

tl-bplus-tree.js

4 B+ Tree Implementations from trincot on StackOverflow
JavaScript
1
star
46

example-c-library

Complicated nested-folder-structure C library example for SO
C
1
star
47

rel.js

Relational Algebra Data Models Experiment in JavaScript
1
star
48

awesome-abstractions

A curated list of awesome API abstractions across languages
1
star
49

mac-keyboard.js

Mac Keyboard .keylayout File Generator
JavaScript
1
star
50

text.js

Language Text Transformation
JavaScript
1
star
51

md2docx.js

Markdown to DOCX in TypeScript
TypeScript
1
star