Skip to content

Commit fa78c8f

Browse files
committed
export AdminAuthApiClient class
1 parent 39f4bbb commit fa78c8f

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

README.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,16 @@ See the [ID Token section of the OpenID Connect spec](http://openid.net/specs/op
129129
- `idToken` The ID token to verify.
130130
- `env` is an optional parameter. but this is using to detect should use emulator or not.
131131

132-
### `WorkersKVStoreSingle.getOrInitialize(cacheKey: string, cfKVNamespace: KVNamespace): WorkersKVStoreSingle`
133-
134-
WorkersKVStoreSingle is created as a singleton object. This is because the Module Worker syntax only use environment variables at the time of request.
132+
### `authObj.verifySessionCookie(sessionCookie: string, env?: EmulatorEnv): Promise<FirebaseIdToken>`
135133

136-
This caches the public key used to verify the Firebase ID token in the [Workers KV](https://developers.cloudflare.com/workers/runtime-apis/kv/).
134+
Verifies a Firebase session cookie. Returns a Promise with the cookie claims. Rejects the promise if the cookie could not be verified.
137135

138-
This is implemented `KeyStorer` interface.
136+
See [Verify Session Cookies](https://firebase.google.com/docs/auth/admin/manage-cookies#verify_session_cookie_and_check_permissions) for code samples and detailed documentation.
139137

140-
- `cacheKey` specifies the key of the public key cache.
141-
- `cfKVNamespace` specifies the KV namespace which is bound your workers.
138+
- `sessionCookie` The session cookie to verify.
139+
- `env` is an optional parameter. but this is using to detect should use emulator or not.
142140

143-
### `createSessionCookie(idToken: string, sessionCookieOptions: SessionCookieOptions, env?: EmulatorEnv): Promise<string>`
141+
### `authObj.createSessionCookie(idToken: string, sessionCookieOptions: SessionCookieOptions, env?: EmulatorEnv): Promise<string>`
144142

145143
Creates a new Firebase session cookie with the specified options. The created JWT string can be set as a server-side session cookie with a custom cookie policy, and be used for session management. The session cookie JWT will have the same payload claims as the provided ID token. See [Manage Session Cookies](https://firebase.google.com/docs/auth/admin/manage-cookies) for code samples and detailed documentation.
146144

@@ -150,14 +148,24 @@ Creates a new Firebase session cookie with the specified options. The created JW
150148

151149
**Required** service acccount credential to use this API. You need to set the credentials with `Auth.getOrInitialize`.
152150

153-
### `verifySessionCookie(sessionCookie: string, env?: EmulatorEnv): Promise<FirebaseIdToken>`
151+
### `WorkersKVStoreSingle.getOrInitialize(cacheKey: string, cfKVNamespace: KVNamespace): WorkersKVStoreSingle`
154152

155-
Verifies a Firebase session cookie. Returns a Promise with the cookie claims. Rejects the promise if the cookie could not be verified.
153+
WorkersKVStoreSingle is created as a singleton object. This is because the Module Worker syntax only use environment variables at the time of request.
156154

157-
See [Verify Session Cookies](https://firebase.google.com/docs/auth/admin/manage-cookies#verify_session_cookie_and_check_permissions) for code samples and detailed documentation.
155+
This caches the public key used to verify the Firebase ID token in the [Workers KV](https://developers.cloudflare.com/workers/runtime-apis/kv/).
158156

159-
- `sessionCookie` The session cookie to verify.
160-
- `env` is an optional parameter. but this is using to detect should use emulator or not.
157+
This is implemented `KeyStorer` interface.
158+
159+
- `cacheKey` specifies the key of the public key cache.
160+
- `cfKVNamespace` specifies the KV namespace which is bound your workers.
161+
162+
### `AdminAuthApiClient.getOrInitialize(projectId: string, credential: Credential, retryConfig?: RetryConfig): AdminAuthApiClient`
163+
164+
AdminAuthApiClient is created as a singleton object. This is because the Module Worker syntax only use environment variables at the time of request.
165+
166+
You can send request with the [Admin Auth API](https://cloud.google.com/identity-platform/docs/reference/rest). To generate an access token, you will use the `Credential` class. For instance, if you want to generate an access token from a Service Account JSON, you need to specify `ServiceAccountCredential` as a parameter during initialization.
167+
168+
By specifying the [`roles/firebaseauth.admin`](https://firebase.google.com/docs/projects/iam/roles-predefined-product#app-distro) role to the Service Account, it becomes available for use. If you want finer control over permissions, create a Custom Role based on the [Access Control](https://cloud.google.com/identity-platform/docs/access-control) guide and assign it to the Service Account.
161169

162170
### `emulatorHost(env?: EmulatorEnv): string | undefined`
163171

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"lint": "eslint --ext .ts .",
2626
"lint-fix": "eslint --fix --ext .ts .",
2727
"prepublish": "run-p build:*",
28-
"wrangler": "wrangler"
28+
"wrangler": "wrangler",
29+
"version": "pnpm run build && git add -A dist"
2930
},
3031
"devDependencies": {
3132
"@cloudflare/workers-types": "^4.20240208.0",

src/index.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { BaseAuth } from './auth';
2+
import { AuthApiClient } from './auth-api-requests';
3+
import type { RetryConfig } from './client';
24
import type { Credential } from './credential';
35
import type { KeyStorer } from './key-store';
46
import { WorkersKVStore } from './key-store';
@@ -8,17 +10,25 @@ export { emulatorHost, useEmulator } from './emulator';
810
export type { KeyStorer };
911
export type { EmulatorEnv } from './emulator';
1012
export type { FirebaseIdToken } from './token-verifier';
13+
export type { RetryConfig };
1114

1215
export class Auth extends BaseAuth {
1316
private static instance?: Auth;
17+
private static withCredential?: Auth;
1418

1519
private constructor(projectId: string, keyStore: KeyStorer, credential?: Credential) {
1620
super(projectId, keyStore, credential);
1721
}
1822

1923
static getOrInitialize(projectId: string, keyStore: KeyStorer, credential?: Credential): Auth {
24+
if (!Auth.withCredential && credential !== undefined) {
25+
Auth.withCredential = new Auth(projectId, keyStore, credential);
26+
}
27+
if (Auth.withCredential) {
28+
return Auth.withCredential;
29+
}
2030
if (!Auth.instance) {
21-
Auth.instance = new Auth(projectId, keyStore, credential);
31+
Auth.instance = new Auth(projectId, keyStore);
2232
}
2333
return Auth.instance;
2434
}
@@ -38,3 +48,18 @@ export class WorkersKVStoreSingle extends WorkersKVStore {
3848
return WorkersKVStoreSingle.instance;
3949
}
4050
}
51+
52+
export class AdminAuthApiClient extends AuthApiClient {
53+
private static instance?: AdminAuthApiClient;
54+
55+
private constructor(projectId: string, credential: Credential, retryConfig?: RetryConfig) {
56+
super(projectId, credential, retryConfig);
57+
}
58+
59+
static getOrInitialize(projectId: string, credential: Credential, retryConfig?: RetryConfig) {
60+
if (!AdminAuthApiClient.instance) {
61+
AdminAuthApiClient.instance = new AdminAuthApiClient(projectId, credential, retryConfig);
62+
}
63+
return AdminAuthApiClient.instance;
64+
}
65+
}

0 commit comments

Comments
 (0)