forked from samvera/browse-everything
-
Notifications
You must be signed in to change notification settings - Fork 1
Sharepoint integration #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
621587c
Bring in sharepoint code
masaball cbc4534
Implement authorization_code flow
masaball be4bbdf
Update SharePoint.md
masaball db8614d
Return all user accessible sharepoint sites
masaball f1de817
Add an option to filter what sites get returned
masaball 02e38d7
Use select param to limit returned metadata
masaball 13d6f0f
Update documentation
masaball eefa721
Merge pull request #3 from avalonmediasystem/sharepoint_sites
masaball f96acc8
Add tests for sharepoint driver
masaball a2dd4c1
Enable permissions re-consent flow
masaball ca6c7da
Use joinedTeams as base folder instead of sites
masaball a8453e9
Merge pull request #4 from avalonmediasystem/sharepoint_teams
masaball 83d18fa
Update SharePoint.md
masaball File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,28 @@ | ||
# Sharepoint Provider | ||
|
||
This provider will allow browse-everything to access Sharepoint on behalf of a specific user. It routes through the `/me/joinedTeams` and `/me/drives` Graph API endpoints, so will list Teams that the user belongs to and the user's personal drives at the top level. Within each Team, it will expand to list any child drives or files that the user has permission to access. | ||
|
||
https://learn.microsoft.com/en-us/graph/auth-v2-user?tabs=http | ||
|
||
Prerequisite: | ||
* App must be registered in the Entra Admin center to receive client_id, client_secret, and tenant_id. | ||
* If using .default endpoint as your scope, you must register API permissions for your application. Minimum permissions: | ||
* Files.Read | ||
* Files.Read.All | ||
* Files.Read.Selected | ||
* offline_access | ||
* openid | ||
* profile | ||
* Team.ReadBasic.All | ||
* User.Read | ||
|
||
To use the sharepoint provider add the following to `config/browse_everything_providers.yml`: | ||
|
||
``` | ||
sharepoint: | ||
client_id: MyAppClientID | ||
client_secret: MyAppClientSecret | ||
tenant_id: MyAzureTenantID | ||
redirect_uri: https://avalon_example.com/browse/connect | ||
scope: offline_access https://graph.microsoft.com/.default | ||
``` |
This file contains hidden or 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 hidden or 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,149 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'oauth2' | ||
|
||
# BrowseEverything OAuth2 session for | ||
# Sharepoint provider | ||
module BrowseEverything | ||
module Auth | ||
module Sharepoint | ||
class Session | ||
OAUTH2_URLS = { | ||
site: 'https://login.microsoftonline.com' | ||
}.freeze | ||
|
||
def initialize(opts = {}) | ||
token_info = opts[:access_token]&.symbolize_keys | ||
|
||
if opts[:client_id] | ||
@oauth2_client = OAuth2::Client.new(opts[:client_id], | ||
opts[:client_secret], | ||
{ | ||
authorize_url: authorize_url(opts[:tenant_id]), | ||
token_url: token_url(opts[:tenant_id]), | ||
redirect_uri: opts[:redirect_uri], | ||
scope: opts[:scope] | ||
}.merge!(OAUTH2_URLS.dup)) | ||
return if token_info.blank? | ||
@access_token = OAuth2::AccessToken.new(@oauth2_client, | ||
token_info[:token], | ||
{ | ||
refresh_token: token_info[:refresh_token], | ||
expires_in: token_info[:expires_in] | ||
}) | ||
end | ||
end | ||
|
||
def authorize_url(tenant_id) | ||
tenant_id + "/oauth2/v2.0/authorize" | ||
end | ||
|
||
def token_url(tenant_id) | ||
tenant_id + "/oauth2/v2.0/token" | ||
end | ||
|
||
def get_access_token(code) | ||
@access_token = @oauth2_client.auth_code.get_token(code) | ||
end | ||
|
||
def refresh_token | ||
@access_token = @access_token.refresh! | ||
end | ||
|
||
def build_auth_header | ||
"BoxAuth api_key=#{@api_key}&auth_token=#{@auth_token}" | ||
end | ||
|
||
# TODO: Figure out if these HTTP related methods are actually necessary | ||
def get(url, raw = false) | ||
uri = URI.parse(url) | ||
request = Net::HTTP::Get.new(uri.request_uri) | ||
request(uri, request, raw) | ||
end | ||
|
||
def delete(url, raw = false) | ||
uri = URI.parse(url) | ||
request = Net::HTTP::Delete.new(uri.request_uri) | ||
request(uri, request, raw) | ||
end | ||
|
||
def request(uri, request, raw = false, retries = 0) | ||
http = Net::HTTP.new(uri.host, uri.port) | ||
http.use_ssl = true | ||
# http.set_debug_output($stdout) | ||
|
||
if @access_token | ||
request.add_field('Authorization', "Bearer #{@access_token.token}") | ||
else | ||
request.add_field('Authorization', build_auth_header) | ||
end | ||
|
||
request.add_field('As-User', @as_user.to_s) if @as_user | ||
|
||
response = http.request(request) | ||
|
||
if response.is_a? Net::HTTPNotFound | ||
raise RubyBox::ObjectNotFound | ||
end | ||
|
||
# Got unauthorized (401) status, try to refresh the token | ||
if response.code.to_i == 401 && @refresh_token && retries.zero? | ||
refresh_token(@refresh_token) | ||
return request(uri, request, raw, retries + 1) | ||
end | ||
|
||
sleep(@backoff) # try not to excessively hammer API. | ||
|
||
handle_errors(response, raw) | ||
end | ||
|
||
def do_stream(url, opts) | ||
params = { | ||
content_length_proc: opts[:content_length_proc], | ||
progress_proc: opts[:progress_proc] | ||
} | ||
|
||
params['Authorization'] = if @access_token | ||
"Bearer #{@access_token.token}" | ||
else | ||
build_auth_header | ||
end | ||
|
||
params['As-User'] = @as_user if @as_user | ||
|
||
open(url, params) | ||
end | ||
|
||
# rubocop: disable Metrics/CyclomaticComplexity | ||
def handle_errors(response, raw) | ||
status = response.code.to_i | ||
body = response.body | ||
begin | ||
parsed_body = JSON.parse(body) | ||
rescue | ||
msg = body.presence || "no data returned" | ||
parsed_body = { "message" => msg } | ||
end | ||
|
||
# status is used to determine whether | ||
# we need to refresh the access token. | ||
parsed_body["status"] = status | ||
|
||
case status / 100 | ||
when 3 | ||
# 302 Found. We should return the url | ||
parsed_body["location"] = response["Location"] if status == 302 | ||
when 4 | ||
raise(RubyBox::ItemNameInUse.new(parsed_body, status, body), parsed_body["message"]) if parsed_body["code"] == "item_name_in_use" | ||
raise(RubyBox::AuthError.new(parsed_body, status, body), parsed_body["message"]) if parsed_body["code"] == "unauthorized" || status == 401 | ||
raise(RubyBox::RequestError.new(parsed_body, status, body), parsed_body["message"]) | ||
when 5 | ||
raise(RubyBox::ServerError.new(parsed_body, status, body), parsed_body["message"]) | ||
end | ||
raw ? body : parsed_body | ||
end | ||
# rubocop: enable Metrics/CyclomaticComplexity | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.