ruby-office365 0.1.3 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d2c2997d3d775d3450dd11a6774e3edee59fc22b307b85b7d525ac561cd19eeb
4
- data.tar.gz: 02bf02e037717417d889e4a81bbead7ead87210390adfb57f9d1643d851b905e
3
+ metadata.gz: 86ee47df4ac96c36024adb5c0595afa78889dc9659aebcdbdf01a59e407a0db9
4
+ data.tar.gz: 42ab9271c0d32b6035ad9caecc837137774bde57d0894fd49f34782d8c0f1f1b
5
5
  SHA512:
6
- metadata.gz: e33d6e7f5d01292a86ecfd6dd70b432f8d4ed88cccdc427a2c09a0c9506610242444a1f49bf48f1c95a306213c7d11d995af5dabcb0fc76073749ea8d0aca14a
7
- data.tar.gz: 707cd85b26cf08352590ae658cc76f981b0cca8a8c3ded186d44cc712e4ee334bf6ef59bc89ac5097f33441e4ea4c49f7f306dcd51d87d300832650fe25bdeb2
6
+ metadata.gz: 8dd5a3b6d974b91bf6c2846b1b9b3f433fcc1abe11d6dfc85f3fe3a7b023313db0079b6056a1e27d3d8a36506a434a4ef0e45f327f4d2dd25b0416f681ff5906
7
+ data.tar.gz: fc2997832814ec584184584546ca7443841914191056f5fd3c080017cf21b273a05f9dd5612b928ad7570fb3cbc202ff0cbaba01e86a62b250c409f1e87776b9
data/.rubocop.yml CHANGED
@@ -20,3 +20,6 @@ Style/Documentation:
20
20
 
21
21
  Metrics/BlockLength:
22
22
  Max: 150
23
+
24
+ Metrics/MethodLength:
25
+ Max: 50
data/CHANGELOG.md CHANGED
@@ -21,4 +21,11 @@
21
21
 
22
22
  - Integrate REST API to get contacts
23
23
  - get profile `client.contacts`
24
- - get contacts data with next link `client.contacts({next_link: 'xxx'})`
24
+ - get contacts data with next link `client.contacts({next_link: 'xxx'})`
25
+
26
+ ## [0.1.5] - (2022-10-27)
27
+
28
+ - Generate URLs for token and able to refresh token
29
+ - get authorize URL `client.authorize_url`
30
+ - get token URL `client.token_url`
31
+ - be able to refresh token `client.refresh_token!`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-office365 (0.1.3)
4
+ ruby-office365 (0.1.5)
5
5
  faraday
6
6
  faraday_middleware
7
7
 
data/README.md CHANGED
@@ -132,6 +132,18 @@ irb(main):018:0> response[:results][0].as_json
132
132
  }
133
133
  ```
134
134
 
135
+ **Refresh User Token**
136
+
137
+ ```ruby
138
+ irb(main):005:0> response = client.refresh_token!
139
+ irb(main):005:0> response.scope
140
+ => "openid User.Read profile email"
141
+ irb(main):005:0> response.access_token
142
+ => "eyJ0eXAiOiJKV1QiLCJub25jZSI6ImFDYUladFJ6M3RSc3dFaktxUHdGbF9kVlFmbjJabG85Mjlkb2xaeFBhZm8iLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGxWMFRPSSIsImtpZCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGxWMFRPSSJ9..."
143
+ irb(main):005:0> response.refresh_token
144
+ => "0.ARgA7EiQdLv1qECnFqPfrznKsT9ERYaGfG9Ki5WzQtEllj8YAJk.AgABAAEAAAD--DLA3VO7QrddgJg7WevrAgDs_wQA9P-Q1ODlBsrdZi-5s2mfLtEsavBgiEhGcz1KEf26fMrGFU3LM_og5l6wjSAtQ83XHLuje0_KYGol26_LGV_uH0F1MwCFR1N3ctwg4_...."
145
+ ```
146
+
135
147
  ## Copyright
136
148
 
137
149
  Copyright (c) 2022 Encore Shao. See LICENSE for details.
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Office365
4
+ module Models
5
+ class AccessToken < Base
6
+ # attr_accessor :token_type, :scope, :expires_in, :ext_expires_in, :access_token, :refresh_token
7
+ end
8
+ end
9
+ end
@@ -10,5 +10,6 @@ module Office365
10
10
  autoload :Owner, "office365/models/owner"
11
11
  autoload :EmailAddress, "office365/models/email_address"
12
12
  autoload :Contact, "office365/models/contact"
13
+ autoload :AccessToken, "office365/models/access_token"
13
14
  end
14
15
  end
@@ -16,37 +16,41 @@ module Office365
16
16
  end
17
17
 
18
18
  def get(uri, args: {})
19
- r_uri = URI(uri.start_with?("https") ? uri : (Office365::API_HOST + uri))
19
+ req_url = URI(uri.start_with?("https") ? uri : (Office365::API_HOST + uri))
20
20
 
21
- response = Faraday.new(url: [r_uri.scheme, "://", r_uri.hostname].join, headers: headers) do |faraday|
21
+ response = Faraday.new(url: [req_url.scheme, "://", req_url.hostname].join, headers: headers) do |faraday|
22
22
  faraday.adapter Faraday.default_adapter
23
23
  faraday.response :json
24
24
  faraday.response :logger, ::Logger.new($stdout), bodies: true if dev_developement?
25
- end.get(r_uri.request_uri, *args)
25
+ end.get(req_url.request_uri, *args)
26
26
 
27
- resp_body = response.body
28
- return resp_body if response.status == 200
29
- raise InvalidAuthenticationTokenError, resp_body.dig("error", "message") if response.status == 401
30
-
31
- raise Error, resp_body["error"]
27
+ parse_respond(response)
32
28
  end
33
29
 
34
30
  def post(uri, args)
35
- response = Faraday.new(url: Office365::API_HOST, headers: headers) do |faraday|
31
+ req_url = URI(uri.start_with?("https") ? uri : (Office365::API_HOST + uri))
32
+
33
+ response = Faraday.new(url: [req_url.scheme, "://", req_url.hostname].join, headers: post_headers) do |faraday|
36
34
  faraday.adapter Faraday.default_adapter
37
35
  faraday.response :json
38
36
  faraday.response :logger, ::Logger.new($stdout), bodies: true if dev_developement?
39
- end.post(uri, args)
37
+ end.post(req_url.request_uri, args.to_query)
38
+
39
+ parse_respond(response)
40
+ end
40
41
 
42
+ private
43
+
44
+ def parse_respond(response)
41
45
  resp_body = response.body
46
+
42
47
  return resp_body if response.status == 200
43
48
  raise InvalidAuthenticationTokenError, resp_body.dig("error", "message") if response.status == 401
49
+ raise InvaliRequestError, resp_body["error_description"] if response.status == 400
44
50
 
45
51
  raise Error, resp_body["error"]
46
52
  end
47
53
 
48
- private
49
-
50
54
  def headers
51
55
  {
52
56
  "Content-Type" => "application/json",
@@ -57,6 +61,12 @@ module Office365
57
61
  def dev_developement?
58
62
  debug
59
63
  end
64
+
65
+ def post_headers
66
+ {
67
+ "Content-Type" => "application/x-www-form-urlencoded"
68
+ }
69
+ end
60
70
  end
61
71
  end
62
72
  end
@@ -3,29 +3,41 @@
3
3
  module Office365
4
4
  module REST
5
5
  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 = {
9
9
  client_id: client_id,
10
10
  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
13
17
 
14
- post(oauth_client.token_url, args)
18
+ [base_uri, "?", azure_params].join
15
19
  end
16
20
 
17
- private
21
+ def token_url
22
+ [LOGIN_HOST, "/#{tenant_id}/oauth2/v2.0/token"].join
23
+ end
18
24
 
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
25
+ def refresh_token!
26
+ Models::AccessToken.new(
27
+ Request.new(nil, debug: debug).post(token_url, token_params)
27
28
  )
28
29
  end
30
+
31
+ private
32
+
33
+ def token_params
34
+ {
35
+ refresh_token: refresh_token,
36
+ client_id: client_id,
37
+ client_secret: client_secret,
38
+ grant_type: "refresh_token"
39
+ }
40
+ end
29
41
  end
30
42
  end
31
43
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Office365
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.5"
5
5
  end
data/lib/office365.rb CHANGED
@@ -8,15 +8,14 @@ require "office365/rest"
8
8
  require "office365/models"
9
9
 
10
10
  module Office365
11
- API_HOST = "https://graph.microsoft.com"
12
- LOGIN_HOST = "https://login.microsoftonline.com/"
11
+ class Error < StandardError; end
12
+ class InvalidAuthenticationTokenError < StandardError; end
13
+ class InvaliRequestError < StandardError; end
13
14
 
14
- AUTHORIZE_URL = "common/oauth2/authorize"
15
- TOKEN_URL = "common/oauth2/token"
15
+ API_HOST = "https://graph.microsoft.com"
16
16
  API_VERSION = "v1.0"
17
- SCOPE = "User.read Calendars.read Mail.ReadBasic Contacts.Read"
18
17
 
19
- class Error < StandardError; end
20
- class InvalidAuthenticationTokenError < StandardError; end
18
+ LOGIN_HOST = "https://login.microsoftonline.com"
19
+ SCOPE = "offline_access User.read Calendars.read Mail.ReadBasic Contacts.Read"
21
20
  # Your code goes here...
22
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-office365
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Encore Shao
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-26 00:00:00.000000000 Z
11
+ date: 2022-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -113,6 +113,7 @@ files:
113
113
  - lib/office365.rb
114
114
  - lib/office365/client.rb
115
115
  - lib/office365/models.rb
116
+ - lib/office365/models/access_token.rb
116
117
  - lib/office365/models/calendar.rb
117
118
  - lib/office365/models/concerns/base.rb
118
119
  - lib/office365/models/contact.rb