-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount_information_access_authorization.rb
139 lines (123 loc) · 3.96 KB
/
account_information_access_authorization.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
require "json"
require "uri"
require "rest-client"
class AccountInformationAccessAuthorization
def initialize(financial_institution_id:, user_login:, user_password:, account_references:, account_information_access_request_redirect_link:, host: "sandbox-authorization.ibanity.com")
@financial_institution_id = financial_institution_id
@user_login = user_login
@user_password = user_password
@account_references = account_references
@account_information_access_request_redirect_link = account_information_access_request_redirect_link
@host = host
end
def execute
get_redirect_parameters
create_authentication
create_verification
link_access_request_to_user
authorize_account_access
sign_access_request
end
private
def get_redirect_parameters
query = {
method: :get,
url: @account_information_access_request_redirect_link,
max_redirects: 0
}
begin
RestClient::Request.execute(query)
rescue RestClient::ExceptionWithResponse => err
end
redirect = err.response.headers[:location]
redirect_parameters = URI.decode_www_form(URI.parse(redirect).query).to_h
@redirect_uri = redirect_parameters["redirectUri"]
@application_id = redirect_parameters["applicationId"]
@sandbox_account_information_access_request_id = redirect.match(/\/account-information-access-requests\/(.+)\?/)[1]
end
def create_authentication
payload = {
data: {
attributes: {
applicationId: @application_id,
login: @user_login,
password: @user_password
}
}
}
response = consent_api_call(method: :post, endpoint: "authentications", payload: payload)
@authentication_id = response["data"]["id"]
end
def create_verification
payload = {
data: {
attributes: {
applicationId: @application_id,
id: @authentication_id,
response: "123456"
}
}
}
response = consent_api_call(method: :post, endpoint: "verifications", payload: payload)
@session_token = response["data"]["attributes"]["sessionToken"]
end
def link_access_request_to_user
consent_api_call(
method: :patch,
endpoint: "account-information-access-requests/#{@sandbox_account_information_access_request_id}/relationships/user",
headers: true,
skip_parse: true
)
end
def authorize_account_access
consent_api_call(
method: :patch,
endpoint: "account-information-access-requests/#{@sandbox_account_information_access_request_id}",
headers: true,
payload: {
data: {
type: "accountInformationAccessRequest",
attributes: {
requestedAccountReferences: @account_references
}
}
},
skip_parse: true
)
end
def sign_access_request
response = consent_api_call({
method: :post,
endpoint: "account-information-access-requests/#{@sandbox_account_information_access_request_id}/signatures",
headers: true,
payload: {
data: {
type: "accountInformationAccessRequest",
attributes: {
redirectUri: @redirect_uri,
response: "123456"
}
}
}
})
redirect_url = response["data"]["attributes"]["redirectUri"]
begin
RestClient::Request.execute({ method: :get, max_redirects: 0, url: redirect_url })
rescue RestClient::ExceptionWithResponse => err
end
end
def consent_api_call(method:, endpoint:, headers: false, payload: {}, skip_parse: false)
basic_headers = {
content_type: :json,
accept: :json
}
basic_headers.merge!({ authorization: "Bearer #{@session_token}"}) if headers
response = RestClient::Request.execute({
method: method,
url: "https://#{@host}/api/financial-institutions/#{@financial_institution_id}/#{endpoint}",
payload: payload ? payload.to_json : nil,
headers: basic_headers
})
JSON.parse(response) unless skip_parse
end
end