Skip to content

Commit 6074cca

Browse files
authored
Merge pull request #16 from Code-Hex/add/revoke
added function to check revoke (breaking changes)
2 parents ac80e34 + fbd71b6 commit 6074cca

File tree

10 files changed

+1571
-53
lines changed

10 files changed

+1571
-53
lines changed

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,24 @@ Auth is created as a singleton object. This is because the Module Worker syntax
120120

121121
See official document for project ID: https://firebase.google.com/docs/projects/learn-more#project-identifiers
122122

123-
### `authObj.verifyIdToken(idToken: string, env?: EmulatorEnv): Promise<FirebaseIdToken>`
123+
### `authObj.verifyIdToken(idToken: string, checkRevoked?: boolean, env?: EmulatorEnv): Promise<FirebaseIdToken>`
124124

125125
Verifies a Firebase ID token (JWT). If the token is valid, the promise is fulfilled with the token's decoded claims; otherwise, the promise is rejected.
126126

127127
See the [ID Token section of the OpenID Connect spec](http://openid.net/specs/openid-connect-core-1_0.html#IDToken) for more information about the specific properties below.
128128

129129
- `idToken` The ID token to verify.
130+
- `checkRevoked` - Whether to check if the session cookie was revoked. This requires an extra request to the Firebase Auth backend to check the `tokensValidAfterTime` time for the corresponding user. When not specified, this additional check is not performed.
130131
- `env` is an optional parameter. but this is using to detect should use emulator or not.
131132

132-
### `authObj.verifySessionCookie(sessionCookie: string, env?: EmulatorEnv): Promise<FirebaseIdToken>`
133+
### `authObj.verifySessionCookie(sessionCookie: string, checkRevoked?: boolean, env?: EmulatorEnv): Promise<FirebaseIdToken>`
133134

134135
Verifies a Firebase session cookie. Returns a Promise with the cookie claims. Rejects the promise if the cookie could not be verified.
135136

136137
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.
137138

138139
- `sessionCookie` The session cookie to verify.
140+
- `checkRevoked` - Whether to check if the session cookie was revoked. This requires an extra request to the Firebase Auth backend to check the `tokensValidAfterTime` time for the corresponding user. When not specified, this additional check is not performed.
139141
- `env` is an optional parameter. but this is using to detect should use emulator or not.
140142

141143
### `authObj.createSessionCookie(idToken: string, sessionCookieOptions: SessionCookieOptions, env?: EmulatorEnv): Promise<string>`
@@ -148,6 +150,28 @@ Creates a new Firebase session cookie with the specified options. The created JW
148150

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

153+
### `authObj.getUser(uid: string, env?: EmulatorEnv): Promise<UserRecord>`
154+
155+
Gets the user data for the user corresponding to a given `uid`.
156+
157+
- `uid` corresponding to the user whose data to fetch.
158+
- `env` is an optional parameter. but this is using to detect should use emulator or not.
159+
160+
### `authObj.revokeRefreshTokens(uid: string, env?: EmulatorEnv): Promise<void>`
161+
162+
Revokes all refresh tokens for an existing user.
163+
164+
- `uid` corresponding to the user whose refresh tokens are to be revoked.
165+
- `env` is an optional parameter. but this is using to detect should use emulator or not.
166+
167+
### `authObj.setCustomUserClaims(uid: string, customUserClaims: object | null, env?: EmulatorEnv): Promise<void>`
168+
169+
Sets additional developer claims on an existing user identified by the provided `uid`, typically used to define user roles and levels of access. These claims should propagate to all devices where the user is already signed in (after token expiration or when token refresh is forced) and the next time the user signs in. If a reserved OIDC claim name is used (sub, iat, iss, etc), an error is thrown. They are set on the authenticated user's ID token JWT.
170+
171+
- `uid` - The `uid` of the user to edit.
172+
- `customUserClaims` The developer claims to set. If null is passed, existing custom claims are deleted. Passing a custom claims payload larger than 1000 bytes will throw an error. Custom claims are added to the user's ID token which is transmitted on every authenticated request. For profile non-access related user attributes, use database or other separate storage systems.
173+
- `env` is an optional parameter. but this is using to detect should use emulator or not.
174+
151175
### `WorkersKVStoreSingle.getOrInitialize(cacheKey: string, cfKVNamespace: KVNamespace): WorkersKVStoreSingle`
152176

153177
WorkersKVStoreSingle is created as a singleton object. This is because the Module Worker syntax only use environment variables at the time of request.
@@ -236,4 +260,4 @@ Access to `/admin/login` after started up Emulator and created an account (email
236260

237261
### Required service account key.
238262

239-
- [ ] Check authorized user is deleted (revoked)
263+
- [x] Check authorized user is deleted (revoked)

example/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Hono } from 'hono';
22
import { getCookie, setCookie } from 'hono/cookie';
33
import { csrf } from 'hono/csrf';
44
import { html } from 'hono/html';
5-
import { Auth, EmulatorCredential, emulatorHost, WorkersKVStoreSingle } from '../src';
5+
import { Auth, ServiceAccountCredential, emulatorHost, WorkersKVStoreSingle, AdminAuthApiClient } from '../src';
66

77
type Env = {
88
EMAIL_ADDRESS: string;
@@ -12,6 +12,9 @@ type Env = {
1212
PUBLIC_JWK_CACHE_KEY: string;
1313

1414
FIREBASE_AUTH_EMULATOR_HOST: string; // satisfied EmulatorEnv
15+
// Set JSON as string.
16+
// See: https://cloud.google.com/iam/docs/keys-create-delete
17+
SERVICE_ACCOUNT_JSON: string;
1518
};
1619

1720
const app = new Hono<{ Bindings: Env }>();
@@ -46,7 +49,7 @@ app.post('/verify-header', async c => {
4649
c.env.PROJECT_ID,
4750
WorkersKVStoreSingle.getOrInitialize(c.env.PUBLIC_JWK_CACHE_KEY, c.env.PUBLIC_JWK_CACHE_KV)
4851
);
49-
const firebaseToken = await auth.verifyIdToken(jwt, c.env);
52+
const firebaseToken = await auth.verifyIdToken(jwt, false, c.env);
5053

5154
return new Response(JSON.stringify(firebaseToken), {
5255
headers: {
@@ -153,16 +156,13 @@ app.post('/admin/login_session', async c => {
153156
// The session cookie will have the same claims as the ID token.
154157
// To only allow session cookie setting on recent sign-in, auth_time in ID token
155158
// can be checked to ensure user was recently signed in before creating a session cookie.
156-
const auth = Auth.getOrInitialize(
159+
const auth = AdminAuthApiClient.getOrInitialize(
157160
c.env.PROJECT_ID,
158-
WorkersKVStoreSingle.getOrInitialize(c.env.PUBLIC_JWK_CACHE_KEY, c.env.PUBLIC_JWK_CACHE_KV),
159-
new EmulatorCredential() // You MUST use ServiceAccountCredential in real world
161+
new ServiceAccountCredential(c.env.SERVICE_ACCOUNT_JSON)
160162
);
161163
const sessionCookie = await auth.createSessionCookie(
162164
idToken,
163-
{
164-
expiresIn,
165-
},
165+
expiresIn,
166166
c.env // This valus must be removed in real world
167167
);
168168
setCookie(c, 'session', sessionCookie, {
@@ -178,13 +178,13 @@ app.get('/admin/profile', async c => {
178178

179179
const auth = Auth.getOrInitialize(
180180
c.env.PROJECT_ID,
181-
WorkersKVStoreSingle.getOrInitialize(c.env.PUBLIC_JWK_CACHE_KEY, c.env.PUBLIC_JWK_CACHE_KV),
182-
new EmulatorCredential() // You MUST use ServiceAccountCredential in real world
181+
WorkersKVStoreSingle.getOrInitialize(c.env.PUBLIC_JWK_CACHE_KEY, c.env.PUBLIC_JWK_CACHE_KV)
183182
);
184183

185184
try {
186185
const decodedToken = await auth.verifySessionCookie(
187186
session,
187+
false,
188188
c.env // This valus must be removed in real world
189189
);
190190
return c.json(decodedToken);

example/wrangler.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ tsconfig = "./tsconfig.json"
1818
# FIREBASE_AUTH_EMULATOR_HOST = ""
1919
FIREBASE_AUTH_EMULATOR_HOST = "127.0.0.1:9099"
2020

21+
# See: https://cloud.google.com/iam/docs/keys-create-delete
22+
SERVICE_ACCOUNT_JSON = "{\"type\":\"service_account\",\"project_id\":\"project12345\",\"private_key_id\":\"xxxxxxxxxxxxxxxxx\",\"private_key\":\"-----BEGIN PRIVATE KEY-----XXXXXX-----END PRIVATE KEY-----\n\",\"client_email\":\"xxxxx@xxxxxx.iam.gserviceaccount.com\",\"client_id\":\"xxxxxx\",\"auth_uri\":\"https://accounts.google.com/o/oauth2/auth\",\"token_uri\":\"https://oauth2.googleapis.com/token\",\"auth_provider_x509_cert_url\":\"https://www.googleapis.com/oauth2/v1/certs\",\"client_x509_cert_url\":\"https://www.googleapis.com/robot/v1/metadata/x509/xxxxx@xxxxxx.iam.gserviceaccount.com\"}"
23+
2124
# Setup user account in Emulator UI
2225
EMAIL_ADDRESS = "test@example.com"
2326
PASSWORD = "test1234"

0 commit comments

Comments
 (0)