Skip to content

Commit 75e4e46

Browse files
committed
Initial commit
0 parents  commit 75e4e46

File tree

10 files changed

+185
-0
lines changed

10 files changed

+185
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp
18+
*.bundle
19+
*.so
20+
*.o
21+
*.a
22+
mkmf.log

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in omniauth-microsoft-office365.gemspec
4+
gemspec

LICENSE.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2018 Akash Kamboj
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# omniauth-onedrive

Rakefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "bundler/gem_tasks"
2+
require "rspec/core/rake_task"
3+
4+
RSpec::Core::RakeTask.new(:spec)
5+
6+
task :default => :spec
7+

lib/omniauth-onedrive.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require "omniauth/onedrive/version"
2+
require "omniauth/strategies/onedrive"

lib/omniauth/onedrive/version.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module OmniAuth
2+
module Onedrive
3+
VERSION = "0.0.2"
4+
end
5+
end

lib/omniauth/strategies/onedrive.rb

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
require "omniauth/strategies/oauth2"
2+
3+
module OmniAuth
4+
module Strategies
5+
class Onedrive < OmniAuth::Strategies::OAuth2
6+
option :name, :onedrive
7+
8+
DEFAULT_SCOPE="openid email profile files.readwrite.all offline_access"
9+
10+
option :client_options, {
11+
site: "https://login.microsoftonline.com",
12+
authorize_url: "/common/oauth2/v2.0/authorize",
13+
token_url: "/common/oauth2/v2.0/token"
14+
}
15+
16+
option :authorize_options, [:scope]
17+
18+
uid { raw_info["id"] }
19+
20+
info do
21+
{
22+
# email: raw_info["EmailAddress"],
23+
# display_name: raw_info["DisplayName"],
24+
# first_name: first_last_from_display_name(raw_info["DisplayName"])[0],
25+
# last_name: first_last_from_display_name(raw_info["DisplayName"])[1],
26+
# image: avatar_file,
27+
# alias: raw_info["Alias"]
28+
}
29+
end
30+
31+
extra do
32+
{
33+
"raw_info" => raw_info
34+
}
35+
end
36+
37+
def raw_info
38+
@raw_info ||= access_token.get("https://graph.microsoft.com/v1.0/me").parsed
39+
end
40+
41+
def authorize_params
42+
super.tap do |params|
43+
%w[display scope auth_type].each do |v|
44+
if request.params[v]
45+
params[v.to_sym] = request.params[v]
46+
end
47+
end
48+
49+
params[:scope] ||= DEFAULT_SCOPE
50+
end
51+
end
52+
53+
private
54+
55+
def first_last_from_display_name(display_name)
56+
# For display names with last name first like "Del Toro, Benicio"
57+
if last_first = display_name.match(/^([^,]+),\s+(\S+)$/)
58+
[last_first[2], last_first[1]]
59+
else
60+
display_name.split(/\s+/, 2)
61+
end
62+
end
63+
64+
def callback_url
65+
options[:redirect_uri] || (full_host + script_name + callback_path)
66+
end
67+
68+
def avatar_file
69+
photo = access_token.get("https://graph.microsoft.com/v1.0/me/photo/$value")
70+
ext = photo.content_type.sub("image/", "") # "image/jpeg" => "jpeg"
71+
72+
Tempfile.new(["avatar", ".#{ext}"]).tap do |file|
73+
file.binmode
74+
file.write(photo.body)
75+
file.rewind
76+
end
77+
78+
rescue ::OAuth2::Error => e
79+
if e.response.status == 404 # User has no avatar...
80+
return nil
81+
elsif e.code['code'] == 'GetUserPhoto' && e.code['message'].match('not supported')
82+
nil
83+
else
84+
raise
85+
end
86+
end
87+
88+
end
89+
end
90+
end

omniauth-onedrive.gemspec

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# coding: utf-8
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'omniauth/onedrive/version'
5+
6+
Gem::Specification.new do |spec|
7+
spec.name = "omniauth-onedrive"
8+
spec.version = OmniAuth::Onedrive::VERSION
9+
spec.authors = ["Akash Kamboj"]
10+
spec.email = ["[email protected]"]
11+
spec.summary = %q{OmniAuth provider for OneDrive}
12+
spec.description = %q{OmniAuth provider for OneDrive}
13+
spec.homepage = "https://github.com/akashkamboj/omniauth-onedrive-oauth2"
14+
spec.license = "MIT"
15+
16+
spec.files = `git ls-files -z`.split("\x0")
17+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19+
spec.require_paths = ["lib"]
20+
21+
spec.required_ruby_version = '>= 2.1'
22+
23+
spec.add_runtime_dependency "omniauth"
24+
spec.add_runtime_dependency "omniauth-oauth2"
25+
26+
spec.add_development_dependency "bundler", ">= 1.6"
27+
spec.add_development_dependency "rake", ">= 11.1.2"
28+
spec.add_development_dependency "rspec", ">= 3.4.0"
29+
spec.add_development_dependency "pry", ">= 0.10.3"
30+
end

0 commit comments

Comments
 (0)