Skip to content

Commit 44558fa

Browse files
author
Encore Shao
committed
feat: add authorize_url and token_url for refresh token
1 parent 2fc83e8 commit 44558fa

5 files changed

Lines changed: 45 additions & 20 deletions

File tree

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ Style/Documentation:
2020

2121
Metrics/BlockLength:
2222
Max: 150
23+
24+
Metrics/MethodLength:
25+
Max: 50

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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

137143
Copyright (c) 2022 Encore Shao. See LICENSE for details.

lib/office365.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99

1010
module 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

lib/office365/rest/token.rb

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,33 @@
33
module 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

spec/office365/client_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
RSpec.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
@@ -67,4 +68,16 @@
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
7083
end

0 commit comments

Comments
 (0)