-
Notifications
You must be signed in to change notification settings - Fork 1
/
wnsm-b2b.js
213 lines (188 loc) · 5.97 KB
/
wnsm-b2b.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const { default: axios } = require('axios')
const { CookieJar } = require('tough-cookie')
const { wrapper } = require('axios-cookiejar-support')
const cheerio = require('cheerio')
const url = require('node:url')
const qs = require('node:querystring')
const config = {
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
logwienBaseUrl: 'https://log.wien/auth/realms/logwien/protocol/openid-connect',
clientId: 'wn-smartmeter-b2b',
wnsmPortalUrl: 'https://smartmeter-business.wienernetze.at/',
wnsmBaseUrl: 'https://api.wstw.at/gateway/WN_SMART_METER_PORTAL_API_B2B/1.0'
}
module.exports = class WnsmB2b {
credentials
http
tokens
apiKey
constructor(username, password) {
this.credentials = {
username, password
}
}
async call(endpoint, params) {
if (!this.tokens) {
// Initial authentication
await this.#authenticate()
} else if (this.tokens) {
const now = Math.round(Date.now() / 1000)
if (now < this.tokens.expires_at) {
// Tokens valid, do nothing
} else if (now < this.tokens.refresh_expires_at) {
// Access token expired, but refresh token still valid
try {
// Refresh tokens
await this.#refreshToken()
} catch (e) {
try {
// Re-authenticate if token refresh fails
await this.#authenticate()
} catch (e) {
throw new Error('Token refresh and re-authentication failed. Cannot authenticate.')
}
}
} else {
// Token and refresh token invalid, re-authenticate
await this.#authenticate()
}
}
let result
try {
result = await this.#call2(endpoint, params)
} catch (e) {
// If call fails, try again with new authentication
await this.#authenticate()
result = await this.#call2(endpoint, params)
}
return result.data
}
async #call2(endpoint, params) {
return this.http.get(
`${config.wnsmBaseUrl}${endpoint}`,
{
headers: {
'Accept': 'application/json',
'Authorization': `Bearer ${this.tokens.access_token}`,
'User-Agent': config.userAgent,
'X-Gateway-APIKey': this.apiKey
},
params
}
)
}
async #authenticate() {
this.http = wrapper(axios.create({
headers: {
'User-Agent': config.userAgent
},
jar: new CookieJar(),
timeout: 60 * 1000
}))
try {
var { data: loginPageHtml } = await this.http.get(
`${config.logwienBaseUrl}/auth`,
{
params: {
'client_id': config.clientId,
'redirect_uri': config.wnsmPortalUrl,
'response_mode': 'fragment',
'response_type': 'code',
'scope': 'openid',
'nonce': ''
}
}
)
} catch (e) {
throw new Error(`Failed to retrieve login page: ${e.response.status} ${e.response.statusText}`)
}
try {
const $ = cheerio.load(loginPageHtml)
const loginUrl = $('form#kc-login-form').attr('action')
var { headers } = await this.http.post(
loginUrl,
this.credentials,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
maxRedirects: 0,
validateStatus: status => status >= 300 && status <= 399
}
)
} catch (e) {
throw new Error(`Login failed - wrong credentials or application changed`)
}
// Obtain tokens
const code = qs.parse(url.parse(headers?.location).hash)?.code
try {
var { data: tokens } = await this.http.post(
`${config.logwienBaseUrl}/token`,
{
code,
'grant_type': 'authorization_code',
'client_id': config.clientId,
'redirect_uri': config.wnsmPortalUrl
}, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
)
} catch (e) {
throw new Error(`Failed to obtain tokens: ${e.response.status} ${e.response.statusText}`)
}
tokens.expires_at = this.#parseJwt(tokens.access_token).exp
tokens.refresh_expires_at = this.#parseJwt(tokens.refresh_token).exp
if (!tokens.expires_at || !tokens.refresh_expires_at) {
throw new Error(`Failed to get token expiration values`)
}
// Obtain api key
try {
const { data: portalPageHtml } = await this.http.get(
config.wnsmPortalUrl,
{
headers: {
'Authorization': `Bearer ${tokens.access_token}`
}
}
)
const mainjsFilename = portalPageHtml.match(/\"(main\.[^\.]+\.js)\"/m)[1]
var { data: mainjs } = await this.http.get(`${config.wnsmPortalUrl}${mainjsFilename}`)
} catch (e) {
throw new Error(`Failed to obtain API key files: ${e.response.status} ${e.response.statusText}`)
}
this.apiKey = mainjs.match(/b2bApiKey:\"([^"]+)\"/m)[1]
if (!this.apiKey) {
throw new Error(`Failed to parse API key`)
}
this.tokens = tokens
}
async #refreshToken() {
try {
var { data: tokens } = await this.http.post(
`${config.logwienBaseUrl}/token`,
{
'grant_type': 'refresh_token',
'refresh_token': this.tokens.refresh_token,
'client_id': config.clientId,
}, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
)
} catch (e) {
throw new Error(`Failed to refresh tokens: ${e.response.status} ${e.response.statusText}`)
}
tokens.expires_at = this.#parseJwt(tokens.access_token).exp
tokens.refresh_expires_at = this.#parseJwt(tokens.refresh_token).exp
if (!tokens.expires_at || !tokens.refresh_expires_at) {
throw new Error(`Failed to get refreshed token expiration values`)
}
this.tokens = tokens
}
#parseJwt(token) {
return JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString())
}
}