-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Dolski
committed
Apr 15, 2020
1 parent
0ea504d
commit 0f1f592
Showing
17 changed files
with
333 additions
and
12 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class BoxController < ApplicationController | ||
|
||
## | ||
# Box OAuth2 callback route. Responds to `GET /box/callback` | ||
# | ||
def oauth_callback | ||
raise ActionController::BadRequest, "Missing code" if params[:code].blank? | ||
code = params[:code].gsub(/^A-Za-z0-9/, "")[0..64] | ||
BoxClient.new(session).new_access_token(code) | ||
redirect_to params[:state] || redirect_path | ||
end | ||
|
||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
## | ||
# Convenience class used for obtaining and refreshing OAuth access tokens from | ||
# Box and/or the session. | ||
# | ||
# # Usage | ||
# | ||
# If {access_token} returns a non-`nil` value, use it. Otherwise, | ||
# obtain a new token via {new_access_token}. | ||
# | ||
class BoxClient | ||
|
||
## | ||
# @param callback_url [String] URL to redirect to. | ||
# @param return_url [String] Information to add to the `state` argument. | ||
# @return [String] | ||
# | ||
def self.authorization_url(callback_url:, state:) | ||
config = ::Configuration.instance | ||
"https://account.box.com/api/oauth2/authorize?response_type=code" + | ||
"&client_id=#{config.box[:client_id]}" + | ||
"&redirect_url=#{callback_url}" + | ||
"&state=#{state}" | ||
end | ||
|
||
def initialize(session) | ||
@session = session | ||
end | ||
|
||
## | ||
# Fetches the access token from the session. If the token is expired, it is | ||
# refreshed, stored, and returned. If there is no token in the session, `nil` | ||
# is returned. | ||
# | ||
# @return [Hash] Hash with the same structure as the one returned from | ||
# {new_access_token}. | ||
# | ||
def access_token | ||
if @session['box'].present? | ||
if Time.now > @session['box']['expires'] | ||
token = refresh_access_token(@session['box']['refresh_token']) | ||
@session['box'] = token | ||
return token | ||
else | ||
return @session['box'] | ||
end | ||
end | ||
nil | ||
end | ||
|
||
## | ||
# Exchanges the OAuth access code (supplied as a query argument to the OAuth | ||
# callback URL) for an access token, stores it in the session, and returns | ||
# it. | ||
# | ||
# @param code [String] | ||
# @return [Hash] Hash with `:access_token`, `:expires`, and `:refresh_token` | ||
# keys. | ||
# @see https://developer.box.com/reference/post-oauth2-token/ | ||
# | ||
def new_access_token(code) | ||
config = ::Configuration.instance | ||
body = { | ||
client_id: config.box[:client_id], | ||
client_secret: config.box[:client_secret], | ||
code: code, | ||
grant_type: "authorization_code" | ||
} | ||
response = post("https://account.box.com/api/oauth2/token", body) | ||
token = token_from_response(response.body) | ||
@session['box'] = token | ||
token | ||
end | ||
|
||
private | ||
|
||
def post(url, body) | ||
headers = { 'Content-Type': "application/x-www-form-urlencoded" } | ||
HTTPClient.new.post(url, body, headers) | ||
end | ||
|
||
## | ||
# @param refresh_token [String] | ||
# | ||
def refresh_access_token(refresh_token) | ||
config = ::Configuration.instance | ||
body = { | ||
client_id: config.box[:client_id], | ||
client_secret: config.box[:client_secret], | ||
refresh_token: refresh_token, | ||
grant_type: "refresh_token" | ||
} | ||
response = post("https://api.box.com/oauth2/token", body) | ||
token_from_response(response.body) | ||
end | ||
|
||
## | ||
# @param entity [String] HTTP response entity a.k.a. body. | ||
# @return [Hash] Hash with `:access_token`, `:expires`, and `:refresh_token` | ||
# keys. | ||
# | ||
def token_from_response(entity) | ||
struct = JSON.parse(entity) | ||
{ | ||
access_token: struct['access_token'], | ||
expires: Time.now + struct['expires_in'], | ||
refresh_token: struct['refresh_token'] | ||
} | ||
end | ||
|
||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
- if @access_token.blank? | ||
.text-center | ||
= link_to BoxClient::authorization_url(callback_url: box_callback_url, | ||
state: request.original_url), | ||
class: "btn btn-primary" do | ||
%i.fa.fa-sign-in-alt | ||
Log into Box | ||
- else | ||
-# Read by JavaScript | ||
= hidden_field_tag "box_access_token", @access_token | ||
|
||
#box-container | ||
-# JavaScript takes over from here. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-# This input is hidden via CSS. JavaScript sends #file-dropzone click events | ||
-# to it in order to open a file selection dialog. | ||
%input#file-chooser{type: "file", multiple: true} | ||
|
||
#file-drop-zone.bg-light | ||
%i.fa.fa-upload | ||
Attach files (not folders) by dropping them here or selecting them. |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Qr3MvjqlnpbgLS3J7EZdPMf89uzTPO/LEB6W8RSeeOTWQpxx77n48jLcsnpaqfFcHuLFn14FBWPNG/sQA1jEsM829R+Yc963kBTv7iTI38tKocyt6gsNM5z5fCOdeq+Uo1pt6fbcI6MKjBl3BCmWPepqoG1GMUMo5GPZo5lAErJeo38uvJEehCluiIyJ5KH74WMK/nwFFFF8rq+tms+RCzOC5UjkD/YA0eF/gEw3N0zxr2td4hwAVL8o8uO2YTVHj532tdcWdDFHyb9SFUC8grzlCCCsuwrO3ew7iERlrax18TXE6i8J+cp+VQ6wA3kbFkV0D70f0teVGBzLDoAs1dke4/k3DGlYorfwT6aupp7PqXofefgr0cSOh2QqbVif9D5owhG0jAtQyGBYo0uLdNXCAoIwPqT3Bu4rhKA3iJqxKQ3kykWtPI7lofVgl6a8ISItQt/H1+7wD5MiYQGVRwPbSG/VjBBeswjCPSVSBm5XNlVnxU9M8ZJ8cdeaCEoyXgUewGekedB/37oZGo+7E0aXom15WuryGaazhSIWbXvWugDVt90LcCQmKVMk/q6ULwerIDH+2xA+ta7ZRmch6bXVy9FdA7HzEUGAsRKK3w1u1EcyRa76PiYeG3TFbUNQdIc58xdtx2E5OD82lanA626+/7VC47Sq5yovTd1Rf3sHsMwPywohoqdts0iNYjyOviocfRAlNSlYNotFVsw4hqCdDG3kgsrLqCbhWV8lkrYc7V9p146LskYDJ1XV2OwIPzLrLSHLdgkCJRFrxfKwcf+LHQeZA0PV23qyPpJ+aKqO1UkmIVDvd2F1jIPB7PiZKGwhWTfkbInU8QNzzcRPyipTLeRVYRUw78fXTr1cNgiXOng+XL3m7Jo9OjegFfINOQpw5aJcu6lLScKqunZ+ByXizAjEDdKF5KY+MV/HJZLzX+DbrkWwvTxb16rJQsfqfEHZSvqMvm0oeA4hlJGa60fj+Km5851I1UCXkzYKhlp9J5q93LqcKoWGsbeAj7M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re3x54TPMwG/es0DpDyhhWwXXfjKjGw6R4JdLihuwIKu1bkboJbrXhQ2ZPxqb3KE5cjRMbKNWqrP2geR3o9adqQSA9Q1Z/Beqrnt6TiHy5/oipR/wUxyn85HYfRjy1GByurzeYX4+H1CsgN/uBdcQf8vtvOqVwrUHVjcVzYi/Kflkln8PmIf1VsvypiDWxFRS445S1xDCgBuHq6Yeg79qKlxBxFp73Ks7L7ay+f/0atIiJHDFgODEse4zjOQrth4if9zXPN2YkTKWhjZTb7pXhQkh3WMD83vWQTl11FdP7TRY1kJaCW46kT4OA8wIPPK7TnOh9vzcm1M5X/bF8zLu7e4UpRZPztppRzR/ZAwdp7whHhnEUOKNiglQbjPebsVyqe/KwNtoyyX--PS8v2vwpvTyszM4I--mVBHpN0ZxzChPZ26AEzCbA== |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
qnjJHlT7UNhA1yAeNmffuqPdkDxfY7y0fuIP7uZPhEuFF3tcmKud1OL43PGop/zEFeZM3m9Z3IGFx/H8Mu8iOykdVyeiyV0wc7HpwOagg3WaSChTL0RrfgR8UR/TLM5jfxigN97CjJ8RsoVJr1J2OdaQyS/ZKCp5aPvHsnYTT+BuDTL2zMB1WWzrN+OvMSAwgVkycwYJ6fcqCXakzNxv85D+Zf+mt6/WpDIuIooM/rMaar0Aj2t6+0Wldp+MxG1TCX8PQlISVNMWXMpboEwOTxcPQmMDUsruCHeHb/0Jdh9zhKsenfKeaKQpSYA0aoTZ4ZHfjb+xgRWd8KUw6CbO6h2V3DdCTHh6/f+5sCg9jdhPJCCE9RKP0x/W8I9PqWON0T6q3XD1YBFijGKedtOXrbuaA+DW4IsUWUsfN69Y5PPz/Yd06czJMdMGmMyE9y4K/bCnfhY7qkY/dwjGTNJSC9zzCUkuuouDjfbeCGJjKUqyHE9rmLf21ViJdU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jAVBW52e0AX2uKhh3SoLJsR/JfBbEQq3S/1ULnuluYN05nxb3E+2WtFoJxbM6EUXebK1mQqzCZsQ2h7jgj2PkTYCv1TO+POtloY+9vUJD/ifn263hdumYnCJ+BPQ0lb3kzuImNODeiEgG8bWOmxzPQWHZ+yXbj1sR1y4NthbUUh/xV4XM5LbbuU6tz5SL1+rPMeNOFlIyK0TgUgw0dnT3kzpMKoYx+IjLXVlX1QlBMW60oSjojD65A8kHK6pzhmNRGoLksf2XbBcfeo9Y5SWjNkeE/xz8eRUl9gWhYmRwbTQfgavUWk9shmYy1zPU5a+aO8L9GM34QcEYDWqTyl0W6uPq4RkHK8mbhxKE+TxiRdiqXBxBzTym3CtrdG4VvyD09TloM5o0Kdx4ieqUfKogj4oPcyih6UXWv62tJ2VOAEPR+bffcYefh6A+N/+IevIQAz/oMAg9w8TgjvsdaXKRZ8HyQL822a3qk4Uju7bWewFU8W7qVrzoDO/+Azm7fWmj1v14crPbCDgUs+hAYRU/G1RnJ3QWUFomiJFEEMzTRm5LblgzzsYiHpcIBD8LoEXujXyagiS/lmMat3k51y9RVgq9eXtvrhoYln4z+CqaKIUUKl1g8pFi1S77uTlGiSOwzlqbNCVSdzEhU9co719l5WDx2K4XDNdR0NIJ0QBHuW1uplUDv6tM5PphR8BI+N8M4w13PJm3l3G23IzswaaWrhq3P0U4zQLR5Cz8cESLeswTcao2ukNCe2fwHhy5tG2rmMm6Ieyg==--Iaf0ykNNbkSr3SXx--XFFu7MogA+7xdGA1lNd4/A== |
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.