Skip to content

Commit

Permalink
Initial version of the gem
Browse files Browse the repository at this point in the history
  • Loading branch information
murbanski committed Mar 30, 2016
0 parents commit 8dda6b1
Show file tree
Hide file tree
Showing 15 changed files with 268 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
*.bundle
*.so
*.o
*.a
mkmf.log
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
omniauth-microsoft-office365-development
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby-2.3.0-preview1
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: ruby
rvm:
- 2.1.0
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in omniauth-microsoft-office365.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2016 Marcin Urbański

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Omniauth::Microsoft::Office365

Office365 OAuth2 Strategy for OmniAuth.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'omniauth-microsoft-office365'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install omniauth-microsoft-office365

## Usage

```ruby
Rails.application.config.middleware.use OmniAuth::Builder do
provider :microsoft_office365, ENV['OFFICE365_KEY'], ENV['OFFICE365_SECRET']
end
```
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec

2 changes: 2 additions & 0 deletions lib/omniauth-microsoft-office365.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "omniauth/microsoft_office365/version"
require "omniauth/strategies/microsoft_office365"
5 changes: 5 additions & 0 deletions lib/omniauth/microsoft_office365/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Omniauth
module MicrosoftOffice365
VERSION = "0.0.1"
end
end
41 changes: 41 additions & 0 deletions lib/omniauth/strategies/microsoft_office365.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "omniauth/strategies/oauth2"

module OmniAuth
module Strategies
class MicrosoftOffice365 < OmniAuth::Strategies::OAuth2
option :name, :microsoft_office365

option :client_options, {
site: "https://login.microsoftonline.com",
authorize_url: "/common/oauth2/v2.0/authorize",
token_url: "/common/oauth2/v2.0/token"
}

option :authorize_params, {
scope: "openid email profile https://outlook.office.com/contacts.read",
}

uid { raw_info["Id"] }

info do
{
email: raw_info["EmailAddress"],
display_name: raw_info["DisplayName"],
first_name: raw_info["DisplayName"].split(", ")[1],
last_name: raw_info["DisplayName"].split(", ")[0],
alias: raw_info["Alias"]
}
end

extra do
{
"raw_info" => raw_info
}
end

def raw_info
@raw_info ||= access_token.get("https://outlook.office.com/api/v2.0/me/").parsed
end
end
end
end
30 changes: 30 additions & 0 deletions omniauth-microsoft-office365.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'omniauth/microsoft_office365/version'

Gem::Specification.new do |spec|
spec.name = "omniauth-microsoft-office365"
spec.version = Omniauth::MicrosoftOffice365::VERSION
spec.authors = ["Marcin Urbański"]
spec.email = ["[email protected]"]
spec.summary = %q{Omniauth provider for Microsoft Office365}
spec.description = %q{Omniauth provider for Microsoft Office365}
spec.homepage = "https://github.com/murbanski/omniauth-microsoft-office365"
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.required_ruby_version = '>= 2.1'

spec.add_runtime_dependency "omniauth"
spec.add_runtime_dependency "omniauth-oauth2"

spec.add_development_dependency "bundler", ">= 1.6"
spec.add_development_dependency "rake", ">= 11.1.2"
spec.add_development_dependency "rspec", ">= 3.4.0"
spec.add_development_dependency "pry", ">= 0.10.3"
end
87 changes: 87 additions & 0 deletions spec/omniauth/strategies/microsoft_office365_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'spec_helper'

RSpec.describe OmniAuth::Strategies::MicrosoftOffice365 do
let(:request) { double('Request', :params => {}, :cookies => {}, :env => {}) }
let(:options) { { } }

let(:app) do
lambda do
[200, {}, ["Hello."]]
end
end

let(:strategy) do
OmniAuth::Strategies::MicrosoftOffice365.new(app, 'appid', 'secret', options)
end

before do
OmniAuth.config.test_mode = true
allow(strategy).to receive(:request).and_return(request)
end

after do
OmniAuth.config.test_mode = false
end

describe "#name" do
it "returns :microsoft_office365" do
expect(strategy.name).to eq(:microsoft_office365)
end
end

describe "#client_options" do
context "with defaults" do
it "uses correct site" do
expect(strategy.client.site).to eq("https://login.microsoftonline.com")
end

it "uses correct authorize_url" do
expect(strategy.client.authorize_url).to eq("https://login.microsoftonline.com/common/oauth2/v2.0/authorize")
end

it "uses correct token_url" do
expect(strategy.client.token_url).to eq("https://login.microsoftonline.com/common/oauth2/v2.0/token")
end
end

context "with customized client options" do
let(:options) do
{
client_options: {
'site' => 'https://example.com',
'authorize_url' => 'https://example.com/authorize',
'token_url' => 'https://example.com/token',
}
}
end

it "uses customized site" do
expect(strategy.client.site).to eq("https://example.com")
end

it "uses customized authorize_url" do
expect(strategy.client.authorize_url).to eq("https://example.com/authorize")
end

it "uses customized token_url" do
expect(strategy.client.token_url).to eq("https://example.com/token")
end
end
end

describe "#authorize_params" do
let(:options) do
{ authorize_params: { foo: "bar", baz: "zip" } }
end

it "uses correct scope and allows to customize authorization parameters" do
expect(strategy.authorize_params).to match(
"scope" => "openid email profile https://outlook.office.com/contacts.read",
"foo" => "bar",
"baz" => "zip",
"state" => /\A\h{48}\z/
)
end
end

end
15 changes: 15 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require "pry"
require 'omniauth-microsoft-office365'

RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

config.disable_monkey_patching!
config.expose_dsl_globally = false
config.profile_examples = 3
config.order = :random
Kernel.srand config.seed
end

0 comments on commit 8dda6b1

Please sign in to comment.