Skip to content

Commit

Permalink
Merge pull request #26 from omrilotan/typescript
Browse files Browse the repository at this point in the history
Add typescript declaration file
  • Loading branch information
sagi committed Apr 20, 2024
2 parents 5eff431 + e459c3e commit 05eda27
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const getToken = async ({
privateKeyPEM,
payload,
alg = 'RS256',
cryptoImppl = null,
cryptoImpl = null,
headerAdditions = {},
}) => { ... }
```
Expand All @@ -52,7 +52,7 @@ const getTokenFromGCPServiceAccount = async ({
serviceAccountJSON,
aud,
alg = 'RS256',
cryptoImppl = null,
cryptoImpl = null,
expiredAfter = 3600,
headerAdditions = {},
payloadAdditions = {}
Expand Down
141 changes: 141 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
interface HeaderAdditions {
/**
* Cryptographic algorithm used to secure the JWT
* @example "RS256"
*/
alg?: string,
/**
* Type of token
* @example "JWT"
*/
typ?: string,
/**
* Service account's private key ID
*/
kid?: string,
}

/**
* General purpose JWT generation
*/
declare function getToken ({
privateKeyPEM,
payload,
alg,
headerAdditions,
cryptoImpl
}: {
/**
* The private key string in PEM format
*/
privateKeyPEM: string,
payload: {
/**
* Service account's email address (sa.client_email)
*/
iss: string,
/**
* Service account's email address (sa.client_email)
*/
sub: string,
/**
* Current Unix timem when the token was issued (in seconds since epoch)
*/
iat: number,
/**
* The time exactly 3600 seconds after the token was issued, when the JWT expires
*/
exp: number,
/**
* The API endpoint.
* @example https://<SERVICE>.googleapis.com/
*/
aud: string,
/**
* The scope of the token
*/
scope: string
},
/**
* Cryptographic algorithm used to secure the JWT
* @default "RS256"
*/
alg?: string,
/**
* An object with keys and string values to be added to the header of the JWT.
*/
headerAdditions?: HeaderAdditions,
/**
* The crypto implementation to use. Use `null` to use the default implementation.
* @see https://w3c.github.io/webcrypto/#crypto-interface
* @default null
*/
cryptoImpl?: Crypto
}): Promise<string>

/**
* Generate a JWT from a service account JSON
*/
declare function getTokenFromGCPServiceAccount ({
serviceAccountJSON,
aud,
alg,
cryptoImpl,
headerAdditions,
payloadAdditions
}: {
/**
* Structure of a service account JSON
*/
serviceAccountJSON: {
type: string;
project_id: string;
private_key_id: string;
private_key: string;
client_email: string;
client_id: string;
auth_uri: string;
token_uri: string;
auth_provider_x509_cert_url: string;
client_x509_cert_url: string;
},
/**
* The API endpoint.
* @example https://<SERVICE>.googleapis.com/
*/
aud: string,
/**
* Cryptographic algorithm used to secure the JWT
* @default "RS256"
*/
alg?: string,
/**
* The crypto implementation to use. Use `null` to use the default implementation.
* @see https://w3c.github.io/webcrypto/#crypto-interface
* @default null
*/
cryptoImpl?: Crypto,
/**
* The time in seconds after the token was issued when the JWT expires
* @default 3600
*/
expiredAfter?: number,
/**
* An object with keys and string values to be added to the header of the JWT.
*/
headerAdditions?: HeaderAdditions,
/**
* an object with keys and string values to be added to the payload of the JWT.
* @example { scope: 'https://www.googleapis.com/auth/chat.bot' }
*/
payloadAdditions?: Record<string, any>
}): Promise<string>

export { getTokenFromGCPServiceAccount, getToken }

interface commonjsModule {
getTokenFromGCPServiceAccount,
getToken
}

export default commonjsModule;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"author": "Sagi Kedmi <git@sagi.io> (https://sagi.io)",
"homepage": "https://sagi.io",
"main": "index.js",
"type": "commonjs",
"types": "index.d.ts",
"license": "MIT",
"private": false,
"scripts": {
Expand Down

0 comments on commit 05eda27

Please sign in to comment.