Skip to content

Commit

Permalink
feat: language option support
Browse files Browse the repository at this point in the history
* For TokenSigning backwards compatibility, some three-letter language codes are supported when used through hwcrypto.js
* The extension does not set a default language - when language is not specified, the attribute is not included in the native application message

Signed-off-by: Tanel Metsar <[email protected]>
  • Loading branch information
taneltm authored and mrts committed Jun 17, 2021
1 parent 4d361e3 commit d4b2ccb
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ __pycache__

# Vim
*.swp

# Visual Studio Code
.vscode/
8 changes: 6 additions & 2 deletions src/background/actions/TokenSigning/getCertificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ import {
} from "../../../models/TokenSigning/TokenSigningResponse";
import { throwAfterTimeout } from "../../../shared/utils";
import errorToResponse from "./errorToResponse";
import threeLetterLanguageCodes from "./threeLetterLanguageCodes";

export default async function getCertificate(
nonce: string,
sourceUrl: string,
lang?: string,
filter: "AUTH" | "SIGN" = "SIGN",
): Promise<TokenSigningCertResponse | TokenSigningErrorResponse> {
if (lang && Object.keys(threeLetterLanguageCodes).includes(lang)) {
lang = threeLetterLanguageCodes[lang];
}

const nativeAppService = new NativeAppService();

if (filter !== "SIGN") {
Expand All @@ -63,8 +68,7 @@ export default async function getCertificate(
arguments: {
"origin": (new URL(sourceUrl)).origin,

// TODO: Implement i18n in native application
// "lang": lang
...(lang ? { lang } : {}),
},
}),

Expand Down
8 changes: 6 additions & 2 deletions src/background/actions/TokenSigning/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
import { throwAfterTimeout } from "../../../shared/utils";
import errorToResponse from "./errorToResponse";
import digestCommands from "./digestCommands";
import threeLetterLanguageCodes from "./threeLetterLanguageCodes";

export default async function sign(
nonce: string,
Expand All @@ -42,6 +43,10 @@ export default async function sign(
algorithm: string,
lang?: string,
): Promise<TokenSigningSignResponse | TokenSigningErrorResponse> {
if (lang && Object.keys(threeLetterLanguageCodes).includes(lang)) {
lang = threeLetterLanguageCodes[lang];
}

const nativeAppService = new NativeAppService();

try {
Expand All @@ -59,8 +64,7 @@ export default async function sign(
"origin": (new URL(sourceUrl)).origin,
"user-eid-cert": new ByteArray().fromHex(certificate).toBase64(),

// TODO: Implement i18n in native application
// "lang": lang
...(lang ? { lang } : {}),
},
}),
throwAfterTimeout(config.TOKEN_SIGNING_USER_INTERACTION_TIMEOUT, new UserTimeoutError()),
Expand Down
17 changes: 17 additions & 0 deletions src/background/actions/TokenSigning/threeLetterLanguageCodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import TypedMap from "../../../models/TypedMap";

/**
* Map of ISO 639-2 three-letter language codes to ISO 639-1 two-letter language codes.
*
* This is only a partial list used for backwards compatibility.
*
* @see https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
*/
export default {
"est": "et",
"eng": "en",
"rus": "ru",
"lit": "lt",
"lat": "lv",
"tur": "tr",
} as TypedMap<string>;
3 changes: 3 additions & 0 deletions src/background/actions/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default async function authenticate(
headers: TypedMap<string>,
userInteractionTimeout: number,
serverRequestTimeout: number,
lang?: string,
): Promise<object | void> {
let webServerService: WebServerService | undefined;
let nativeAppService: NativeAppService | undefined;
Expand Down Expand Up @@ -93,6 +94,8 @@ export default async function authenticate(
? new ByteArray(response.certificateInfo?.rawDER).toBase64()
: null
),

...(lang ? { lang } : {}),
},
}),

Expand Down
5 changes: 5 additions & 0 deletions src/background/actions/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default async function sign(
headers: TypedMap<string>,
userInteractionTimeout: number,
serverRequestTimeout: number,
lang?: string,
): Promise<object | void> {
let webServerService: WebServerService | undefined;
let nativeAppService: NativeAppService | undefined;
Expand Down Expand Up @@ -69,6 +70,8 @@ export default async function sign(

arguments: {
"origin": (new URL(postPrepareSigningUrl)).origin,

...(lang ? { lang } : {}),
},
}),

Expand Down Expand Up @@ -132,6 +135,8 @@ export default async function sign(
"hash-algo": prepareDocumentResult.body.algorithm,
"origin": (new URL(postPrepareSigningUrl)).origin,
"user-eid-cert": certificate,

...(lang ? { lang } : {}),
},
}),

Expand Down
2 changes: 2 additions & 0 deletions src/background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async function onAction(message: LibraryMessage): Promise<void | object> {
message.headers,
message.userInteractionTimeout || libraryConfig.DEFAULT_USER_INTERACTION_TIMEOUT,
message.serverRequestTimeout || libraryConfig.DEFAULT_SERVER_REQUEST_TIMEOUT,
message.lang,
);

case Action.SIGN:
Expand All @@ -50,6 +51,7 @@ async function onAction(message: LibraryMessage): Promise<void | object> {
message.headers,
message.userInteractionTimeout || libraryConfig.DEFAULT_USER_INTERACTION_TIMEOUT,
message.serverRequestTimeout || libraryConfig.DEFAULT_SERVER_REQUEST_TIMEOUT,
message.lang,
);

case Action.STATUS:
Expand Down
2 changes: 2 additions & 0 deletions src/models/LibraryMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface AuthenticateRequestMessage extends Object {
headers: TypedMap<string>;
userInteractionTimeout: number;
serverRequestTimeout: number;
lang?: string;
}

export interface SignRequestMessage extends Object {
Expand All @@ -48,4 +49,5 @@ export interface SignRequestMessage extends Object {
headers: TypedMap<string>;
userInteractionTimeout: number;
serverRequestTimeout: number;
lang?: string;
}

0 comments on commit d4b2ccb

Please sign in to comment.