-
Notifications
You must be signed in to change notification settings - Fork 47
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 typescript type definitions #7
Open
clems4ever
wants to merge
1
commit into
ashtuchkin:master
Choose a base branch
from
clems4ever:type-definitions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
node_modules | ||
*.sublime-* | ||
*.sublime-* | ||
.vscode | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Type definitions for u2f 0.1.2 | ||
// Project: https://github.com/ashtuchkin/u2f | ||
// Definitions by: Clement Michaud <https://github.com/clems4ever> | ||
// Definitions: https://github.com/ashtuchkin/u2f | ||
// TypeScript Version: 2.1 | ||
|
||
export interface Request { | ||
version: "U2F_V2"; | ||
appId: string; | ||
challenge: string; | ||
keyHandle?: string; | ||
} | ||
|
||
export interface RegistrationData { | ||
clientData: string; | ||
registrationData: string; | ||
errorCode?: number; | ||
} | ||
|
||
export interface RegistrationResult { | ||
successful: true; | ||
publicKey: string; | ||
keyHandle: string; | ||
certificate: Buffer; | ||
} | ||
|
||
export interface SignatureData { | ||
clientData: string; | ||
signatureData: string; | ||
errorCode?: number; | ||
} | ||
|
||
export interface SignatureResult { | ||
successful: boolean; | ||
userPresent: boolean; | ||
counter: number; | ||
} | ||
|
||
export interface Error { | ||
errorCode: number; | ||
errorMessage: string; | ||
} | ||
|
||
export function request(appId: string, keyHandle?: string): Request; | ||
export function checkRegistration(request: Request, registerData: RegistrationData): RegistrationResult | Error; | ||
export function checkSignature(request: Request, signData: SignatureData, publicKey: string): SignatureResult | Error; | ||
|
||
// For testing purposes | ||
export function _toWebsafeBase64(buffer: Buffer): string; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
|
||
import * as u2f from "../index"; | ||
import assert = require("assert"); | ||
|
||
interface RegistrationEntry { | ||
keyHandle: string; | ||
publicKey: string; | ||
} | ||
|
||
const USERNAME = "username"; | ||
|
||
describe("FIDO Specification v1.0-rd-20141008 (Typescript)", function () { | ||
describe("checkRegister", function () { | ||
it("should pass test from spec", function () { | ||
const REGISTRATION_APP_ID = "http://example.com"; | ||
|
||
const certificate = new Buffer("3082013c3081e4a003020102020a47901280001155957352300a06082a8648ce3d0403023017311530130603550403130c476e756262792050696c6f74301e170d3132303831343138323933325a170d3133303831343138323933325a3031312f302d0603550403132650696c6f74476e756262792d302e342e312d34373930313238303030313135353935373335323059301306072a8648ce3d020106082a8648ce3d030107034200048d617e65c9508e64bcc5673ac82a6799da3c1446682c258c463fffdf58dfd2fa3e6c378b53d795c4a4dffb4199edd7862f23abaf0203b4b8911ba0569994e101300a06082a8648ce3d0403020347003044022060cdb6061e9c22262d1aac1d96d8c70829b2366531dda268832cb836bcd30dfa0220631b1459f09e6330055722c8d89b7f48883b9089b88d60d1d9795902b30410df", 'hex'); | ||
const clientData = new Buffer('{"typ":"navigator.id.finishEnrollment","challenge":"vqrS6WXDe1JUs5_c3i4-LkKIHRr-3XVb3azuA5TifHo","cid_pubkey":{"kty":"EC","crv":"P-256","x":"HzQwlfXX7Q4S5MtCCnZUNBw3RMzPO9tOyWjBqRl4tJ8","y":"XVguGFLIZx1fXg3wNqfdbn75hi4-_7-BxhMljw42Ht4"},"origin":"http://example.com"}'); | ||
const publicKey = new Buffer("04b174bc49c7ca254b70d2e5c207cee9cf174820ebd77ea3c65508c26da51b657c1cc6b952f8621697936482da0a6d3d3826a59095daf6cd7c03e2e60385d2f6d9", "hex"); | ||
const keyHandle = new Buffer("2a552dfdb7477ed65fd84133f86196010b2215b57da75d315b7b9e8fe2e3925a6019551bab61d16591659cbaf00b4950f7abfe6660e2e006f76868b772d70c25", "hex"); | ||
const registrationData = new Buffer("0504b174bc49c7ca254b70d2e5c207cee9cf174820ebd77ea3c65508c26da51b657c1cc6b952f8621697936482da0a6d3d3826a59095daf6cd7c03e2e60385d2f6d9402a552dfdb7477ed65fd84133f86196010b2215b57da75d315b7b9e8fe2e3925a6019551bab61d16591659cbaf00b4950f7abfe6660e2e006f76868b772d70c253082013c3081e4a003020102020a47901280001155957352300a06082a8648ce3d0403023017311530130603550403130c476e756262792050696c6f74301e170d3132303831343138323933325a170d3133303831343138323933325a3031312f302d0603550403132650696c6f74476e756262792d302e342e312d34373930313238303030313135353935373335323059301306072a8648ce3d020106082a8648ce3d030107034200048d617e65c9508e64bcc5673ac82a6799da3c1446682c258c463fffdf58dfd2fa3e6c378b53d795c4a4dffb4199edd7862f23abaf0203b4b8911ba0569994e101300a06082a8648ce3d0403020347003044022060cdb6061e9c22262d1aac1d96d8c70829b2366531dda268832cb836bcd30dfa0220631b1459f09e6330055722c8d89b7f48883b9089b88d60d1d9795902b30410df304502201471899bcc3987e62e8202c9b39c33c19033f7340352dba80fcab017db9230e402210082677d673d891933ade6f617e5dbde2e247e70423fd5ad7804a6d3d3961ef871", "hex"); | ||
|
||
// produces RegistrationData | ||
function register_mock(request: u2f.Request): u2f.RegistrationData { | ||
return { | ||
clientData: u2f._toWebsafeBase64(clientData), | ||
registrationData: u2f._toWebsafeBase64(registrationData) | ||
} | ||
} | ||
|
||
function save_mock(username: string, keyHandle: string, publicKey: string): void { | ||
} | ||
|
||
// ********************************************* | ||
// Registration | ||
// ********************************************* | ||
|
||
const registrationRequest = u2f.request(REGISTRATION_APP_ID); | ||
registrationRequest.challenge = "vqrS6WXDe1JUs5_c3i4-LkKIHRr-3XVb3azuA5TifHo"; | ||
// Client parses the request and produces registration data | ||
const registrationResponse = register_mock(registrationRequest); | ||
const registration = u2f.checkRegistration(registrationRequest, registrationResponse); | ||
|
||
|
||
let registrationResult: u2f.RegistrationResult; | ||
if ((<u2f.Error>registration).errorCode) | ||
console.log("Error during registration!"); | ||
else { | ||
registrationResult = <u2f.RegistrationResult>registration; | ||
save_mock(USERNAME, registrationResult.keyHandle, registrationResult.publicKey); | ||
console.log("Registered!"); | ||
} | ||
|
||
assert(registrationResult); | ||
assert.strictEqual(registrationResult.publicKey, u2f._toWebsafeBase64(publicKey)); | ||
assert.strictEqual(registrationResult.keyHandle, u2f._toWebsafeBase64(keyHandle)); | ||
}); | ||
}); | ||
|
||
// ********************************************* | ||
// Signature | ||
// ********************************************* | ||
|
||
describe("checkSignature", function () { | ||
it("should pass test from spec", function () { | ||
const SIGNATURE_APP_ID = "https://gstatic.com/securitykey/a/example.com"; | ||
|
||
const publicKey = new Buffer("04d368f1b665bade3c33a20f1e429c7750d5033660c019119d29aa4ba7abc04aa7c80a46bbe11ca8cb5674d74f31f8a903f6bad105fb6ab74aefef4db8b0025e1d", 'hex'); | ||
const clientData = new Buffer('{"typ":"navigator.id.getAssertion","challenge":"opsXqUifDriAAmWclinfbS0e-USY0CgyJHe_Otd7z8o","cid_pubkey":{"kty":"EC","crv":"P-256","x":"HzQwlfXX7Q4S5MtCCnZUNBw3RMzPO9tOyWjBqRl4tJ8","y":"XVguGFLIZx1fXg3wNqfdbn75hi4-_7-BxhMljw42Ht4"},"origin":"http://example.com"}'); | ||
const signatureData = new Buffer('0100000001304402204b5f0cd17534cedd8c34ee09570ef542a353df4436030ce43d406de870b847780220267bb998fac9b7266eb60e7cb0b5eabdfd5ba9614f53c7b22272ec10047a923f', 'hex'); | ||
|
||
|
||
function sign_mock(request: u2f.Request): u2f.SignatureData { | ||
return { | ||
clientData: u2f._toWebsafeBase64(clientData), | ||
signatureData: u2f._toWebsafeBase64(signatureData) | ||
} | ||
} | ||
|
||
|
||
function load_mock(username: string): RegistrationEntry { | ||
return { | ||
keyHandle: "", | ||
publicKey: u2f._toWebsafeBase64(publicKey) | ||
} | ||
} | ||
|
||
|
||
const savedRegistration = load_mock(USERNAME); | ||
|
||
const signatureRequest = u2f.request(SIGNATURE_APP_ID, savedRegistration.keyHandle); | ||
signatureRequest.challenge = "opsXqUifDriAAmWclinfbS0e-USY0CgyJHe_Otd7z8o"; | ||
|
||
// Client parses the request and produces signature data | ||
const signatureResponse = sign_mock(signatureRequest); | ||
const signature = u2f.checkSignature(signatureRequest, signatureResponse, savedRegistration.publicKey); | ||
|
||
let signatureResult: u2f.SignatureResult; | ||
if ((<u2f.Error>signature).errorCode) | ||
console.log("Error during signature!"); | ||
else { | ||
signatureResult = <u2f.SignatureResult>signature; | ||
if (signatureResult.successful) | ||
console.log("Authenticated!"); | ||
} | ||
|
||
assert(signatureResult); | ||
assert.strictEqual(signatureResult.userPresent, true); | ||
assert.strictEqual(signatureResult.counter, 1); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for writing tests! I really appreciate it.
This file is not using mocha, though, so it can be confusing. Can you try to make it a real test? This article can help: https://journal.artfuldev.com/unit-testing-node-applications-with-typescript-using-mocha-and-chai-384ef05f32b2
Or, if you want to just keep it as an example of how typescript would work, then can you rename this file to
typescript-example.ts
, so it doesn't look like a test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done. One can run the tests with
mocha -r ts-node/register test/ts-test.ts
.