Last active
December 5, 2016 07:28
-
-
Save sitepodmatt/b73e4f38fac13fcc1aab to your computer and use it in GitHub Desktop.
oauth2 experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns acme.sheets | |
(:require [midje.sweet :refer :all]) | |
(:require | |
[clj-time.core :as t] | |
[clj-time.coerce :refer [to-long]] | |
[clj-http.client :as client] | |
[byte-streams :refer [to-input-stream]] | |
[cheshire.core :refer [parse-stream]] | |
[clj-jwt.core :refer [jwt sign to-str]] | |
[clj-jwt.key :refer [private-key]])) | |
(defn read-json [json-file] | |
(-> json-file | |
(clojure.java.io/reader) | |
(parse-stream true))) | |
(defn string->private-key [s] | |
(println s) | |
(-> s | |
(to-input-stream) | |
(private-key))) | |
(defn claim->signed-jwt [private-key claim] | |
(-> claim | |
jwt | |
(sign :RS256 private-key) | |
to-str)) | |
(def ^:dynamic *google-token-url* "https://www.googleapis.com/oauth2/v3/token") | |
(defn test-google [] | |
(let [srv_acc (read-json "priv/corp-c9bff43e9b67.json") | |
now (t/minus (t/now) (t/seconds 1)) | |
payload {:iss (:client_email srv_acc) | |
:scope "https://www.googleapis.com/auth/drive" | |
:iat now | |
:sub "removed@antiscrapers.blah" | |
:exp (t/plus now (t/minutes 60)) | |
:aud "https://www.googleapis.com/oauth2/v3/token" } | |
_ (println payload) | |
signed_payload (-> payload | |
(->> (claim->signed-jwt | |
(string->private-key (:private_key srv_acc)))))] | |
(let [access-token (-> (client/post *google-token-url* {:as :json | |
:form-params | |
{:grant_type "urn:ietf:params:oauth:grant-type:jwt-bearer" | |
:assertion signed_payload }}) | |
:body | |
:access_token | |
)] | |
(client/get "https://www.googleapis.com/drive/v2/files" { :oauth-token access-token }) | |
))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment