Skip to content

Instantly share code, notes, and snippets.

@deadprogram
Created November 19, 2012 00:36
Show Gist options
  • Save deadprogram/4108342 to your computer and use it in GitHub Desktop.
Save deadprogram/4108342 to your computer and use it in GitHub Desktop.

ATT OAuth2 Clojure Client

Example of accessing ATT OAuth in Clojure using noir.

Usage

lein deps
CLIENT_ID=XXX CLIENT_SECRET=YYY AUTH_SERVER=xyz.com lein run

License

Copyright (C) 2012 The Hybrid Group

Distributed under the MIT License

(defproject att-oauth-client "0.1.0-SNAPSHOT"
:description "Example of accessing ATT OAuth in Clojure using noir"
:dependencies [[org.clojure/clojure "1.3.0"]
[noir "1.2.1"]
[org.clojars.deadprogram/oauthentic "1.0.4"]]
:main att-oauth-client.server)
(ns att-oauth-client.server
(:require [noir.server :as server]))
(server/load-views "src/att_oauth_client/views/")
(defn -main [& m]
(let [mode (keyword (or (first m) :dev))
port (Integer. (get (System/getenv) "PORT" "4567"))]
(server/start port {:mode mode
:ns 'att-oauth-client})))
(ns att-oauth-client.views.welcome
(:require [att-oauth-client.views.common :as common]
[clj-http.client :as client])
(:use [noir.core :only [defpage custom-handler]]
[noir.response :only [redirect]]
[hiccup.core :only [html]]
[oauthentic.core]))
(defpage "/" []
(let []
(common/layout
[:div
[:h1 "ATT OAuth Demo"]
[:a {:href "/auth"} "Get Profile"]])))
(defpage "/auth" []
(let [
auth-url (build-authorization-url
{ :authorization-url (str "https://" (get (System/getenv) "AUTH_SERVER") "/oauth/authorize")
:client-id (get (System/getenv) "CLIENT_ID")}
{ :redirect-uri "http://localhost:4567/auth/callback"
:scope :profile })]
(redirect auth-url)))
(defpage "/auth/callback" {:keys [code]}
(let [
token (fetch-token { :token-url (str "https://" (get (System/getenv) "AUTH_SERVER") "/oauth/token")
:client-id (get (System/getenv) "CLIENT_ID")
:client-secret (get (System/getenv) "CLIENT_SECRET")}
{ :code code
:insecure? true
:redirect-uri "http://localhost:4567/auth/callback"})
profile-data (:body (client/get (str "https://" (get (System/getenv) "AUTH_SERVER") "/me.json")
{ :as :json :accept :json :oauth-token (:access-token token) :insecure? true }))]
(common/layout
[:div
[:h1 "profile-data"]
[:p (str profile-data)]])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment