Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for running api-test-app with api-hjarntorget #583

Merged
merged 2 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions apps/api-test-app/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ const path = require('path')
const fs = require('fs')
const HttpProxyAgent = require('https-proxy-agent')
const agentWrapper = require('./app/agentFetchWrapper')
const init = require('@skolplattformen/api-skolplattformen').default
const initSkolplattformen = require('@skolplattformen/api-skolplattformen').default
const initHjarntorget = require('@skolplattformen/api-hjarntorget').default

const [, , personalNumber, platform] = process.argv
const isHjarntorget = platform && platform.startsWith('hj')
const init = isHjarntorget ? initHjarntorget : initSkolplattformen;

const [, , personalNumber] = process.argv
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
const cookieJar = new CookieJar()
let bankIdUsed = false
Expand Down Expand Up @@ -136,10 +140,9 @@ async function Login(api) {
try {
console.log('Attempt to use saved session cookie to login')
const rawContent = await readFile(`${recordFolder}/latestSessionCookie.txt`)
const sessionCookie = JSON.parse(rawContent)

await api.setSessionCookie(`${sessionCookie.key}=${sessionCookie.value}`)

const sessionCookies = JSON.parse(rawContent)
await api.setSessionCookie(`${sessionCookies[0].key}=${sessionCookies[0].value}`)

useBankId = false
console.log('Login with old cookie succeeded')
} catch (error) {
Expand Down Expand Up @@ -177,10 +180,12 @@ function ensureDirectoryExistence(filePath) {
fs.mkdirSync(dirname)
}


function getSessionCookieFromCookieJar() {
const cookies = cookieJar.getCookiesSync('https://etjanst.stockholm.se')
const sessionCookie = cookies.find((c) => c.key === 'SMSESSION')
return sessionCookie
const cookieUrl = isHjarntorget ? 'https://hjarntorget.goteborg.se' : 'https://etjanst.stockholm.se'
const cookies = cookieJar.getCookiesSync(cookieUrl)
const sessionCookieKey = isHjarntorget ? 'JSESSIONID' : 'SMSESSION'
return cookies.find(c => c.key === sessionCookieKey)
}

const record = async (info, data) => {
Expand Down
9 changes: 2 additions & 7 deletions libs/api-hjarntorget/lib/apiHjarntorget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,8 @@ export class ApiHjarntorget extends EventEmitter implements Api {
}

async setSessionCookie(sessionCookie: string): Promise<void> {
await this.fetch('login-cookie', hjarntorgetUrl, {
headers: {
cookie: sessionCookie,
},
redirect: 'manual',
})

this.cookieManager.setCookieString(sessionCookie, hjarntorgetUrl)

const user = await this.getUser()
if (!user.isAuthenticated) {
throw new Error('Session cookie is expired')
Expand Down