Giter VIP home page Giter VIP logo

twitter-friend's Introduction

Twitter OAuth support for Friend

twitter-friend is a library to provide a ready-to-use friend workflow for twitter.

Installation

Add [twitter-friend "0.1.0"] to your leiningen or boot dependencies.

Usage

Twitter uses three-legged oauth, which involves two steps to authenticate the user:

  • The user heads to your :login-uri, and gets redirected to Twitter to authorize your access.
  • After authenticating the user, twitter redirects back to your application.

This library attempts to keep the choices you have to make to a minimum:

  1. You have to provide the URL that twitter redirects to after authorizing.
  2. You have to provide a credential-fn that accepts a twitter access token and returns a friend authorization map.

The credential function receives a map that looks like this:

{:oauth_token "users-twitter-access-token"
 :oauth_token_secret "users-twitter-access-secret"
 :user_id "users-twitter-user-id"
 :screen_name "users-twitter-screen-name"}

Example Usage

(require '[twitter-friend :refer [twitter-auth-workflow]])


;; Your credential function is called when the authorization flow
;; is finished.
(defn my-credential-fn [{:keys [oauth_token
                                oauth_token_secret
                                user_id
                                screen_name] :as auth-response}]
  (let [user (mydb/get-or-create-user auth-response)]
    {:identity (:id user) :roles #{::user} :user user}))


(def my-workflow
  (twitter-auth-workflow-config
    {:consumer-key "MY_TWITTER_CONSUMER_KEY"
    :consumer-secret "MY_TWITTER_CONSUMER_SECRET"
    :oauth-callback-uri {:base-url "http://myapp.com"
                         :path "/path/to/oauth"}
    :credential-fn my-credential-fn}))


(def myapp
  (-> myroutes
      (friend :workflows [my-workflow])
      (wrap-params)
      (wrap-keyword-params)
      (wrap-session)))  ; These middlewares are required

Workflows other than redirect-with-session

If you don't want your app to redirect automatically to twitter, or to use the session middleware (for example, if you're integrating oauth with an ajax call), you can additionally provide two more configuration keys to `twitter-auth-workflow:

authorization-fn must be a function of three arguments that accepts a request-token object, the authorization redirect url, and the incoming Ring request, and returns a ring response. You'll need to eventually redirect your user to the auth-url, and store the request-token in such a way that you can retrieve it later.

request-token-fn accepts a request, and returns the request-token that you stored earlier.

Here are the default implementations, which use ring' session middleware, for reference:

(defn default-authorization-fn [request-token auth-url req]
 (update-in
     (resp/redirect auth-url)
     [:session] assoc :oauth-request-token request-token))


(defn default-request-token-fn [req]
  (get-in req [:session :oauth-request-token]))

You can pass these in as additional keys in the config map passed to twitter-auth-workflow.

twitter-friend's People

Contributors

adambard avatar

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.