Skip to content

Commit 4f7fe2e

Browse files
author
Robert-Jan Huijsman
authored
Auth: reformat URL-formatted UIDs in the event payload. (#141)
Our backend is delivering URL-formatted UIDs (e.g. http://github.com/12345) for sign-ups from external event providers. This is out of spec, and out of sync with the admin SDK. We fix this here in the SDK so that customers will get the format fix when they next deploy. We'll fix the format sent by the backend further into the future; this SDK fix is forwards-compatible for that.
1 parent ffefe6b commit 4f7fe2e

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

spec/providers/auth.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,33 @@ describe('AuthBuilder', () => {
146146

147147
return expect(cloudFunction(event)).to.eventually.deep.equal(event.data);
148148
});
149+
150+
it('should reformat a url-formatted UID', () => {
151+
const cloudFunction = auth.user().onCreate((ev: Event<firebase.auth.UserRecord>) => ev.data);
152+
let event: Event<firebase.auth.UserRecord> = {
153+
data: {
154+
uid: 'http://github.com/abcde12345',
155+
},
156+
} as any;
157+
return expect(cloudFunction(event)).to.eventually.deep.equal(
158+
{
159+
uid: 'abcde12345',
160+
},
161+
);
162+
});
163+
164+
it('should not reformat legally-formatted UIDs', () => {
165+
const cloudFunction = auth.user().onCreate((ev: Event<firebase.auth.UserRecord>) => ev.data);
166+
let event: Event<firebase.auth.UserRecord> = {
167+
data: {
168+
uid: 'abcde12345',
169+
},
170+
} as any;
171+
return expect(cloudFunction(event)).to.eventually.deep.equal(
172+
{
173+
uid: 'abcde12345',
174+
},
175+
);
176+
});
149177
});
150178
});

src/providers/auth.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ export class UserBuilder {
5050
metadata.createdAt = new Date(metadata.createdAt);
5151
}
5252
}
53+
54+
// It is possible that the incoming UID is formatted as a URL, for example: "http://github.com/12345".
55+
// That format would break our spec, since...
56+
// - The Admin SDK would return a UserRecord with a UID of just "12345".
57+
// - The UserRecord UID is supposed to be usable as a key in the Firebase Realtime Database, which is
58+
// impossible with this slash-based format.
59+
// Hence, we'll re-format the UID here, if it was indeed supplied as a URL.
60+
// We won't use string.split(), since it's not available on older versions of node.
61+
// BUG(36486645)
62+
if (raw.data.uid) {
63+
raw.data.uid = raw.data.uid.substring(raw.data.uid.lastIndexOf('/') + 1);
64+
}
65+
5366
return raw.data;
5467
}
5568

0 commit comments

Comments
 (0)