Skip to content

Commit

Permalink
Add Cert Check
Browse files Browse the repository at this point in the history
  • Loading branch information
ingalls committed Jun 24, 2024
1 parent e7c7513 commit eafcbf6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
24 changes: 21 additions & 3 deletions api/lib/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fetch from './fetch.js';
import { CookieJar } from 'tough-cookie';
import { CookieAgent } from 'http-cookie-agent/undici';
import { X509Certificate } from 'crypto';
import TAKAPI, { APIAuthPassword } from '../lib/tak-api.js';
import TAKAPI, { APIAuthPassword, APIAuthCertificate } from '../lib/tak-api.js';

export enum AuthProviderAccess {
ADMIN = 'admin',
Expand Down Expand Up @@ -51,9 +51,9 @@ export default class AuthProvider {
if (split.length < 2) throw new Err(500, null, 'Unexpected TAK JWT Format');
const contents: { sub: string; aud: string; nbf: number; exp: number; iat: number; } = JSON.parse(split[1]);

let profile;
const api = await TAKAPI.init(new URL(this.config.MartiAPI), new APIAuthPassword(username, password));

let profile;
try {
profile = await this.config.models.Profile.from(username);
} catch (err) {
Expand All @@ -79,11 +79,29 @@ export default class AuthProvider {
}
} catch (err) {
console.error(`Error: CertificateExpiration: ${validTo}: ${err}`);
await this.config.models.Profile.commit(username, {

profile = await this.config.models.Profile.commit(username, {
auth: await api.Credentials.generate()
});
}

const cert_api = await TAKAPI.init(new URL(String(this.config.server.api)), new APIAuthCertificate(profile.auth.cert, profile.auth.key));

try {
// No "certificate validity" endpoint exists so make a common call
// to ensure we get a 200 response and not a 500 - Update to check status when Josh
// pushes a fix to throw a 401 instead of a 500 on bad certs
await cert_api.Contacts.list();
} catch (err) {
if (err.message.includes('org.springframework.security.authentication.BadCredentialsException')) {
profile = await this.config.models.Profile.commit(username, {
auth: await api.Credentials.generate()
});
} else {
throw err;
}
}

return contents.sub;
}
}
4 changes: 3 additions & 1 deletion api/routes/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export default async function router(schema: Schema, config: Config) {
console.error(err);
}
} else {
await config.models.Profile.commit(email, { last_login: new Date().toISOString() });
await config.models.Profile.commit(email, {
last_login: new Date().toISOString()
});
}

const profile = await config.models.Profile.from(email);
Expand Down

0 comments on commit eafcbf6

Please sign in to comment.