File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -20,3 +20,6 @@ Style/Documentation:
2020
2121Metrics/BlockLength :
2222 Max : 150
23+
24+ Metrics/MethodLength :
25+ Max : 50
Original file line number Diff line number Diff line change @@ -132,6 +132,12 @@ irb(main):018:0> response[:results][0].as_json
132132}
133133```
134134
135+ ** Refresh User Token**
136+
137+ ``` ruby
138+ irb(main):005 :0 > client.refresh_token
139+ ```
140+
135141## Copyright
136142
137143Copyright (c) 2022 Encore Shao. See LICENSE for details.
Original file line number Diff line number Diff line change 99
1010module Office365
1111 API_HOST = "https://graph.microsoft.com"
12- LOGIN_HOST = "https://login.microsoftonline.com/"
13-
14- AUTHORIZE_URL = "common/oauth2/authorize"
15- TOKEN_URL = "common/oauth2/token"
1612 API_VERSION = "v1.0"
13+
14+ LOGIN_HOST = "https://login.microsoftonline.com"
1715 SCOPE = "User.read Calendars.read Mail.ReadBasic Contacts.Read"
1816
1917 class Error < StandardError ; end
Original file line number Diff line number Diff line change 33module Office365
44 module REST
55 module Token
6- def token_refresh
7- args = {
8- refresh_token : refresh_token ,
6+ def authorize_url
7+ base_uri = [ LOGIN_HOST , "/ #{ tenant_id } /oauth2/v2.0/authorize" ] . join
8+ azure_params = {
99 client_id : client_id ,
1010 client_secret : client_secret ,
11- grant_type : "refresh_token"
12- }
11+ scope : Office365 ::SCOPE ,
12+ response_type : "code" ,
13+ response_mode : "query" ,
14+ redirect_uri : redirect_uri ,
15+ state : SecureRandom . hex
16+ } . to_query
1317
14- post ( oauth_client . token_url , args )
18+ [ base_uri , "?" , azure_params ] . join
1519 end
1620
17- private
21+ def token_url
22+ [ LOGIN_HOST , "/#{ tenant_id } /oauth2/v2.0/token" ] . join
23+ end
1824
19- def oauth_client
20- @oauth_client ||= OAuth2 ::Client . new (
21- client_id ,
22- client_secret ,
23- authorize_url : AUTHORIZE_URL ,
24- site : LOGIN_HOST ,
25- token_url : TOKEN_URL ,
26- redirect_uri : redirect_uri
27- )
25+ def token_refresh
26+ post ( token_url , {
27+ refresh_token : refresh_token ,
28+ client_id : client_id ,
29+ client_secret : client_secret ,
30+ grant_type : "refresh_token" ,
31+ scope : Office365 ::SCOPE
32+ } )
2833 end
2934 end
3035 end
Original file line number Diff line number Diff line change 33RSpec . describe Office365 ::Client do
44 let ( :client ) do
55 Office365 ::REST ::Client . new do |config |
6+ config . tenant_id = "12345"
67 config . access_token = "token"
78 end
89 end
6768
6869 expect ( response [ :results ] . size ) . to eq ( 1 )
6970 end
71+
72+ it "returns authorize URL" do
73+ expect ( client . authorize_url ) . to include ( "https://login.microsoftonline.com/12345/oauth2/v2.0/authorize" )
74+ end
75+
76+ it "returns token URL" do
77+ expect ( client . token_url ) . to eq ( "https://login.microsoftonline.com/12345/oauth2/v2.0/token" )
78+ end
79+
80+ xit "be able to refresh token by refresh_token" do
81+ expect ( client . token_refresh ) . to eq ( { } )
82+ end
7083end
You can’t perform that action at this time.
0 commit comments