Skip to content

Commit f0b80f1

Browse files
authored
Merge pull request #46 from bKP451/feat/support_yammer_app
Support yammer app
2 parents 9836bd5 + ede5808 commit f0b80f1

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

lib/omniauth/strategies/microsoft_graph.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class MicrosoftGraph < OmniAuth::Strategies::OAuth2
66
BASE_SCOPE_URL = 'https://graph.microsoft.com/'
77
BASE_SCOPES = %w[offline_access openid email profile].freeze
88
DEFAULT_SCOPE = 'offline_access openid email profile User.Read'.freeze
9+
YAMMER_PROFILE_URL = 'https://www.yammer.com/api/v1/users/current.json'
10+
MICROSOFT_GRAPH_PROFILE_URL = 'https://graph.microsoft.com/v1.0/me'
911

1012
option :name, :microsoft_graph
1113

@@ -64,7 +66,7 @@ def authorize_params
6466
end
6567

6668
def raw_info
67-
@raw_info ||= access_token.get('https://graph.microsoft.com/v1.0/me').parsed
69+
@raw_info ||= access_token.get(profile_endpoint).parsed
6870
end
6971

7072
def callback_url
@@ -73,11 +75,27 @@ def callback_url
7375

7476
def custom_build_access_token
7577
access_token = get_access_token(request)
78+
# Get the profile(microsoft graph / yammer) endpoint choice based on returned bearer token
79+
@profile_endpoint = determine_profile_endpoint(request)
7680
access_token
7781
end
7882

7983
alias build_access_token custom_build_access_token
8084

85+
def profile_endpoint
86+
@profile_endpoint ||= MICROSOFT_GRAPH_PROFILE_URL
87+
end
88+
89+
def determine_profile_endpoint(request)
90+
scope = request&.env&.dig('omniauth.params', 'scope')
91+
92+
if scope&.include?('yammer')
93+
YAMMER_PROFILE_URL
94+
else
95+
MICROSOFT_GRAPH_PROFILE_URL
96+
end
97+
end
98+
8199
private
82100

83101
def get_access_token(request)

spec/omniauth/strategies/microsoft_graph_oauth2_spec.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,4 +457,82 @@
457457
end.to raise_error(OAuth2::Error)
458458
end
459459
end
460+
461+
describe 'Yammer profile endpoint support' do
462+
describe '#profile_endpoint' do
463+
context 'when no profile endpoint is determined' do
464+
it 'defaults to Microsoft Graph profile URL' do
465+
expect(subject.profile_endpoint).to eq('https://graph.microsoft.com/v1.0/me')
466+
end
467+
end
468+
469+
context 'when profile endpoint is already set' do
470+
before { subject.instance_variable_set(:@profile_endpoint, 'https://custom.endpoint.com') }
471+
472+
it 'returns the previously set endpoint' do
473+
expect(subject.profile_endpoint).to eq('https://custom.endpoint.com')
474+
end
475+
end
476+
end
477+
478+
describe '#determine_profile_endpoint' do
479+
let(:request) { double('Request', env: request_env) }
480+
481+
context 'when scope includes Yammer access_as_user scope' do
482+
let(:request_env) { { 'omniauth.params' => { 'scope' => 'https://api.yammer.com/access_as_user' } } }
483+
484+
it 'returns Yammer profile URL' do
485+
expect(subject.determine_profile_endpoint(request)).to eq('https://www.yammer.com/api/v1/users/current.json')
486+
end
487+
end
488+
489+
context 'when scope includes Yammer user_impersonation scope' do
490+
let(:request_env) { { 'omniauth.params' => { 'scope' => 'openid profile https://api.yammer.com/user_impersonation' } } }
491+
492+
it 'returns Yammer profile URL' do
493+
expect(subject.determine_profile_endpoint(request)).to eq('https://www.yammer.com/api/v1/users/current.json')
494+
end
495+
end
496+
497+
context 'when scope includes Yammer scope among other scopes' do
498+
let(:request_env) { { 'omniauth.params' => { 'scope' => 'offline_access openid email profile https://api.yammer.com/access_as_user User.Read' } } }
499+
500+
it 'returns Yammer profile URL' do
501+
expect(subject.determine_profile_endpoint(request)).to eq('https://www.yammer.com/api/v1/users/current.json')
502+
end
503+
end
504+
505+
context 'when scope includes multiple Yammer scopes' do
506+
let(:request_env) { { 'omniauth.params' => { 'scope' => 'openid profile https://api.yammer.com/access_as_user https://api.yammer.com/user_impersonation' } } }
507+
508+
it 'returns Yammer profile URL' do
509+
expect(subject.determine_profile_endpoint(request)).to eq('https://www.yammer.com/api/v1/users/current.json')
510+
end
511+
end
512+
513+
context 'when scope does not include any Yammer scopes' do
514+
let(:request_env) { { 'omniauth.params' => { 'scope' => 'openid profile User.Read' } } }
515+
516+
it 'returns Microsoft Graph profile URL' do
517+
expect(subject.determine_profile_endpoint(request)).to eq('https://graph.microsoft.com/v1.0/me')
518+
end
519+
end
520+
521+
context 'when scope is nil' do
522+
let(:request_env) { { 'omniauth.params' => { 'scope' => nil } } }
523+
524+
it 'returns Microsoft Graph profile URL' do
525+
expect(subject.determine_profile_endpoint(request)).to eq('https://graph.microsoft.com/v1.0/me')
526+
end
527+
end
528+
529+
context 'when omniauth.params is nil' do
530+
let(:request_env) { { 'omniauth.params' => nil } }
531+
532+
it 'returns Microsoft Graph profile URL' do
533+
expect(subject.determine_profile_endpoint(request)).to eq('https://graph.microsoft.com/v1.0/me')
534+
end
535+
end
536+
end
537+
end
460538
end

0 commit comments

Comments
 (0)