reivt 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,32 +1,32 @@
1
- # An extension of the main module, Revit
2
- #
3
- # @author [brwnrclse]
4
- #
5
- module Reivt
6
- # An extension of the RevAPI module
7
- #
8
- # @author [brwnrclse]
9
- #
10
- module RevAPI
11
- CreateUserMutation = CLIENT.parse <<-'GRAPHQL'
12
- mutation($auth0_id: String!) {
13
- createUser(authProvider:{auth0: {idToken: $auth0_id}}) {
14
- id
15
- }
16
- }
17
- GRAPHQL
18
-
19
- # Authenticate the user against the api to receive a user id
20
- # @param id [String] The users's id from Auth0
21
- #
22
- # @return [String] The user's id in the rev api
23
- #
24
- def self.create_user(auth0_id)
25
- result = CLIENT.query(RevAPI::CreateUserMutation,
26
- variables: { auth0_id: auth0_id })
27
- data = RevAPI.retrieve(result)
28
-
29
- data.createUser.id
30
- end
31
- end
32
- end
1
+ # An extension of the main module, Revit
2
+ #
3
+ # @author [brwnrclse]
4
+ #
5
+ module Reivt
6
+ # An extension of the RevAPI module
7
+ #
8
+ # @author [brwnrclse]
9
+ #
10
+ module RevAPI
11
+ CreateUserMutation = CLIENT.parse <<-'GRAPHQL'
12
+ mutation($auth0_id: String!) {
13
+ createUser(authProvider:{auth0: {idToken: $auth0_id}}) {
14
+ id
15
+ }
16
+ }
17
+ GRAPHQL
18
+
19
+ # Authenticate the user against the api to receive a user id
20
+ # @param id [String] The users's id from Auth0
21
+ #
22
+ # @return [String] The user's id in the rev api
23
+ #
24
+ def self.create_user(auth0_id)
25
+ result = CLIENT.query(RevAPI::CreateUserMutation,
26
+ variables: { auth0_id: auth0_id })
27
+ data = RevAPI.retrieve(result)
28
+
29
+ data.createUser.id
30
+ end
31
+ end
32
+ end
@@ -1,39 +1,39 @@
1
- # An extension of the main module, Revit
2
- #
3
- # @author [brwnrclse]
4
- #
5
- module Reivt
6
- # An extension of the RevAPI module
7
- #
8
- # @author [brwnrclse]
9
- #
10
- module RevAPI
11
- SigninMutation = CLIENT.parse <<-'GRAPHQL'
12
- mutation($auth0_id: String!) {
13
- signinUser(auth0:{idToken: $auth0_id}) {
14
- user {
15
- id
16
- }
17
- }
18
- }
19
- GRAPHQL
20
-
21
- # Authenticate the user against the api to receive a user id
22
- # @param id [String] The users's id from Auth0
23
- #
24
- # @return [String] The user's id in the rev api
25
- #
26
- def self.signin_user(auth0_id)
27
- result = CLIENT.query(RevAPI::SigninMutation,
28
- variables: { auth0_id: auth0_id })
29
-
30
- if result.data.nil?
31
- nil
32
- else
33
- data = RevAPI.retrieve(result)
34
-
35
- data.signinUser.user.id
36
- end
37
- end
38
- end
39
- end
1
+ # An extension of the main module, Revit
2
+ #
3
+ # @author [brwnrclse]
4
+ #
5
+ module Reivt
6
+ # An extension of the RevAPI module
7
+ #
8
+ # @author [brwnrclse]
9
+ #
10
+ module RevAPI
11
+ SigninMutation = CLIENT.parse <<-'GRAPHQL'
12
+ mutation($auth0_id: String!) {
13
+ signinUser(auth0:{idToken: $auth0_id}) {
14
+ user {
15
+ id
16
+ }
17
+ }
18
+ }
19
+ GRAPHQL
20
+
21
+ # Authenticate the user against the api to receive a user id
22
+ # @param id [String] The users's id from Auth0
23
+ #
24
+ # @return [String] The user's id in the rev api
25
+ #
26
+ def self.signin_user(auth0_id)
27
+ result = CLIENT.query(RevAPI::SigninMutation,
28
+ variables: { auth0_id: auth0_id })
29
+
30
+ if result.data.nil?
31
+ nil
32
+ else
33
+ data = RevAPI.retrieve(result)
34
+
35
+ data.signinUser.user.id
36
+ end
37
+ end
38
+ end
39
+ end
data/lib/reivt/api.rb CHANGED
@@ -1,64 +1,64 @@
1
- require 'graphql/client'
2
- require 'graphql/client/http'
3
-
4
- require 'pp'
5
-
6
- # An extension of our main module Reivt
7
- #
8
- # @author [brwnrclse]
9
- #
10
- module Reivt
11
- # Intrface for rev API, connects local changes with our remote.
12
- #
13
- # @author [brwnrclse]
14
- #
15
- module RevAPI
16
- SCHEMA_PATH = "#{Dir.pwd}/lib/reivt/schema/schema.json".freeze
17
- ENDPOINT_URL = 'https://api.graph.cool/simple/v1/revwver'.freeze
18
- ENDPOINT = GraphQL::Client::HTTP.new(ENDPOINT_URL) do
19
- def headers(_context)
20
- auth_token = Reivt::REIVT_STORE.transaction do |store|
21
- store.fetch(:auth0_id, nil)
22
- end
23
-
24
- { 'Authorization': "Bearer #{auth_token}" }
25
- end
26
- end
27
-
28
- ID_TOKEN = Reivt::REIVT_STORE.transaction do |store|
29
- store.fetch(:user_id, nil)
30
- end
31
-
32
- unless File.exist?(SCHEMA_PATH)
33
- GraphQL::Client.dump_schema(ENDPOINT, SCHEMA_PATH)
34
- end
35
-
36
- SCHEMA = GraphQL::Client.load_schema(SCHEMA_PATH)
37
- CLIENT = GraphQL::Client.new(schema: SCHEMA, execute: ENDPOINT)
38
-
39
- # Raise any errors in the response data or simply return the data
40
- #
41
- # @param api_response [Obj] A nested object containing either data or errors
42
- #
43
- # @return [Array] A list containing the data and error object from the req
44
- #
45
- def self.retrieve(api_response)
46
- errors = api_response.errors
47
- data = api_response.data
48
-
49
- if data.nil?
50
- msg = errors.details[:data].map { |error| error['message'] }
51
- raise Reivt::GraphQLDataException, msg.join(",\n")
52
- else
53
- data
54
- end
55
- end
56
- end
57
- end
58
-
59
- require_relative 'api/mutations/document_create.mutation'
60
- require_relative 'api/mutations/document_delete.mutation'
61
- require_relative 'api/mutations/rev_create.mutation'
62
- require_relative 'api/mutations/rev_delete.mutation'
63
- require_relative 'api/mutations/user_create.mutation'
64
- require_relative 'api/mutations/user_signin.mutation'
1
+ require 'graphql/client'
2
+ require 'graphql/client/http'
3
+
4
+ require 'pp'
5
+
6
+ # An extension of our main module Reivt
7
+ #
8
+ # @author [brwnrclse]
9
+ #
10
+ module Reivt
11
+ # Intrface for rev API, connects local changes with our remote.
12
+ #
13
+ # @author [brwnrclse]
14
+ #
15
+ module RevAPI
16
+ SCHEMA_PATH = "#{Dir.pwd}/lib/reivt/schema/schema.json".freeze
17
+ ENDPOINT_URL = 'https://api.graph.cool/simple/v1/revwver'.freeze
18
+ ENDPOINT = GraphQL::Client::HTTP.new(ENDPOINT_URL) do
19
+ def headers(_context)
20
+ auth_token = Reivt::REIVT_STORE.transaction do |store|
21
+ store.fetch(:auth0_id, nil)
22
+ end
23
+
24
+ { 'Authorization': "Bearer #{auth_token}" }
25
+ end
26
+ end
27
+
28
+ ID_TOKEN = Reivt::REIVT_STORE.transaction do |store|
29
+ store.fetch(:user_id, nil)
30
+ end
31
+
32
+ unless File.exist?(SCHEMA_PATH)
33
+ GraphQL::Client.dump_schema(ENDPOINT, SCHEMA_PATH)
34
+ end
35
+
36
+ SCHEMA = GraphQL::Client.load_schema(SCHEMA_PATH)
37
+ CLIENT = GraphQL::Client.new(schema: SCHEMA, execute: ENDPOINT)
38
+
39
+ # Raise any errors in the response data or simply return the data
40
+ #
41
+ # @param api_response [Obj] A nested object containing either data or errors
42
+ #
43
+ # @return [Array] A list containing the data and error object from the req
44
+ #
45
+ def self.retrieve(api_response)
46
+ errors = api_response.errors
47
+ data = api_response.data
48
+
49
+ if data.nil?
50
+ msg = errors.details[:data].map { |error| error['message'] }
51
+ raise Reivt::GraphQLDataException, msg.join(",\n")
52
+ else
53
+ data
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ require_relative 'api/mutations/document_create.mutation'
60
+ require_relative 'api/mutations/document_delete.mutation'
61
+ require_relative 'api/mutations/rev_create.mutation'
62
+ require_relative 'api/mutations/rev_delete.mutation'
63
+ require_relative 'api/mutations/user_create.mutation'
64
+ require_relative 'api/mutations/user_signin.mutation'
data/lib/reivt/auth.rb CHANGED
@@ -1,82 +1,96 @@
1
- require 'base64'
2
- require 'bcrypt'
3
- require 'digest'
4
- require 'json'
5
- require 'net/http'
6
- require 'sysrandom'
7
- require 'uri'
8
-
9
- # An extension of our main module
10
- #
11
- # @author [brwnrclse]
12
- #
13
- module Reivt
14
- # Convience module for handling our authentication actions and talking to
15
- # Auth0
16
- #
17
- # @author [brwnrclse]
18
- #
19
- module Auth
20
- AUTH_CALLBACK_URL = 'https://rev.vaemoi.co/login_success'.freeze
21
- AUTH_CLIENT_ID = 'Q1fRDQ9u3oN33ok0ciIi9Vww5kV8U8MA'.freeze
22
- AUTH0_ID = Reivt::REIVT_STORE.transaction do
23
- Reivt::REIVT_STORE.fetch(:auth0_id, nil)
24
- end
25
- AUTH_STORE_ACCESS_TOKEN = Reivt::REIVT_STORE.transaction do
26
- Reivt::REIVT_STORE.fetch(:access_token, nil)
27
- end
28
- AUTH_URL = 'https://vaemoi.auth0.com'.freeze
29
- VERIFIER = Sysrandom.urlsafe_base64(32)
30
-
31
- # Provides the user with a means to obtain an authorization code for
32
- # accessing rev's api by opening a browser to our Auth0 login page
33
- #
34
- # @return [nil]
35
- #
36
- def self.auth_code_url
37
- verifier_challenge = Sysrandom.urlsafe_base64(
38
- Digest::SHA256.new.update(VERIFIER).digest.to_i
39
- )
40
-
41
- auth_code_url = AUTH_URL +
42
- '/authorize?response_type=code&scope=openid%20profile' \
43
- '&client_id=' + AUTH_CLIENT_ID +
44
- '&redirect_uri=' + AUTH_CALLBACK_URL +
45
- '&code_challenge=' + verifier_challenge +
46
- '&code_challenge_method=S256'
47
-
48
- auth_code_url
49
- end
50
-
51
- # Exchanges the auth code obtained for a token used to access rev's api
52
- #
53
- # @param auth_code [String] The auth code obtained from logging in
54
- #
55
- # @return [String] The auth token used for accessing rev's api
56
- #
57
- def self.auth_token(auth_code)
58
- auth_token_uri = URI.parse('https://vaemoi.auth0.com/oauth/token')
59
- body = {
60
- grant_type: 'authorization_code',
61
- client_id: AUTH_CLIENT_ID,
62
- code_verifier: VERIFIER,
63
- code: auth_code,
64
- redirect_uri: AUTH_CALLBACK_URL
65
- }
66
- http = Net::HTTP.new(auth_token_uri.host, auth_token_uri.port)
67
- http.use_ssl = true
68
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
69
- req = Net::HTTP::Post.new(auth_token_uri)
70
- req.content_type = 'application/json'
71
- req.body = body.to_json
72
-
73
- res = http.request(req)
74
- token = {}
75
- token[:access_token] = JSON.parse(res.body)['access_token']
76
- token[:auth0_id] = JSON.parse(res.body)['id_token']
77
- token[:expires] = Time.now.to_i + JSON.parse(res.body)['expires'].to_i
78
-
79
- token
80
- end
81
- end
82
- end
1
+ require 'base64'
2
+ require 'bcrypt'
3
+ require 'digest'
4
+ require 'json'
5
+ require 'net/http'
6
+ require 'sysrandom'
7
+ require 'uri'
8
+
9
+ # An extension of our main module
10
+ #
11
+ # @author [brwnrclse]
12
+ #
13
+ module Reivt
14
+ # Convience module for handling our authentication actions and talking to
15
+ # Auth0
16
+ #
17
+ # @author [brwnrclse]
18
+ #
19
+ module Auth
20
+ AUTH_CALLBACK_URL = 'https://rev.vaemoi.co/login_success'.freeze
21
+ AUTH_CLIENT_ID = 'Q1fRDQ9u3oN33ok0ciIi9Vww5kV8U8MA'.freeze
22
+ AUTH0_ID = Reivt::REIVT_STORE.transaction do
23
+ Reivt::REIVT_STORE.fetch(:auth0_id, nil)
24
+ end
25
+ AUTH_STORE_ACCESS_TOKEN = Reivt::REIVT_STORE.transaction do
26
+ Reivt::REIVT_STORE.fetch(:access_token, nil)
27
+ end
28
+ AUTH_URL = 'https://vaemoi.auth0.com'.freeze
29
+ VERIFIER = Sysrandom.urlsafe_base64(32)
30
+
31
+ # Provides the user with a means to obtain an authorization code for
32
+ # accessing rev's api by opening a browser to our Auth0 login page
33
+ #
34
+ # @return [nil]
35
+ #
36
+ def self.auth_code_url
37
+ verifier_challenge = Sysrandom.urlsafe_base64(
38
+ Digest::SHA256.new.update(VERIFIER).digest.to_i
39
+ )
40
+
41
+ auth_code_url = AUTH_URL +
42
+ '/authorize?response_type=code&scope=openid%20profile' \
43
+ '&client_id=' + AUTH_CLIENT_ID +
44
+ '&redirect_uri=' + AUTH_CALLBACK_URL +
45
+ '&code_challenge=' + verifier_challenge +
46
+ '&code_challenge_method=S256'
47
+
48
+ auth_code_url
49
+ end
50
+
51
+ # Exchanges the auth code obtained for a token used to access rev's api
52
+ #
53
+ # @param auth_code [String] The auth code obtained from logging in
54
+ #
55
+ # @return [String] The auth token used for accessing rev's api
56
+ #
57
+ def self.auth_token(auth_code)
58
+ auth_token_uri = URI.parse('https://vaemoi.auth0.com/oauth/token')
59
+ body = {
60
+ grant_type: 'authorization_code',
61
+ client_id: AUTH_CLIENT_ID,
62
+ code_verifier: VERIFIER,
63
+ code: auth_code,
64
+ redirect_uri: AUTH_CALLBACK_URL
65
+ }
66
+ http = Net::HTTP.new(auth_token_uri.host, auth_token_uri.port)
67
+ http.use_ssl = true
68
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
69
+ req = Net::HTTP::Post.new(auth_token_uri)
70
+ req.content_type = 'application/json'
71
+ req.body = body.to_json
72
+
73
+ res = http.request(req)
74
+ token = {}
75
+ token[:access_token] = JSON.parse(res.body)['access_token']
76
+ token[:auth0_id] = JSON.parse(res.body)['id_token']
77
+ token[:expires] = Time.now.to_i + JSON.parse(res.body)['expires'].to_i
78
+
79
+ token
80
+ end
81
+
82
+
83
+ # Checks if user has an auth token - if not, they're most likely not logged in
84
+ #
85
+ # @param N/A
86
+ #
87
+ # @return true if logged in
88
+ #
89
+ def self.logged_in
90
+ if AUTH_STORE_ACCESS_TOKEN.nil?
91
+ raise Reivt::LoginException
92
+ end
93
+ return true
94
+ end
95
+ end
96
+ end