Skip to content

Commit

Permalink
s/user/publicKey in Dsn type and class
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsterkatie committed Feb 1, 2021
1 parent d118b85 commit 3c62321
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class API {
const dsn = this._dsnObject;
const header = [`Sentry sentry_version=${SENTRY_API_VERSION}`];
header.push(`sentry_client=${clientName}/${clientVersion}`);
header.push(`sentry_key=${dsn.user}`);
header.push(`sentry_key=${dsn.publicKey}`);
if (dsn.pass) {
header.push(`sentry_secret=${dsn.pass}`);
}
Expand Down Expand Up @@ -134,7 +134,7 @@ export class API {
const auth = {
// We send only the minimum set of required information. See
// https://github.com/getsentry/sentry-javascript/issues/2572.
sentry_key: dsn.user,
sentry_key: dsn.publicKey,
sentry_version: SENTRY_API_VERSION,
};
return urlEncode(auth);
Expand Down
4 changes: 3 additions & 1 deletion packages/types/src/dsn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ export type DsnProtocol = 'http' | 'https';
export interface DsnComponents {
/** Protocol used to connect to Sentry. */
protocol: DsnProtocol;
/** Public authorization key (deprecated, renamed to publicKey). */
user?: string;
/** Public authorization key. */
user: string;
publicKey?: string;
/** Private authorization key (deprecated, optional). */
pass?: string;
/** Hostname of the Sentry instance. */
Expand Down
24 changes: 17 additions & 7 deletions packages/utils/src/dsn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DsnComponents, DsnLike, DsnProtocol } from '@sentry/types';

import { SentryError } from './error';
import { logger } from './logger';

/** Regular expression used to parse a Dsn. */
const DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(?::(\w+))?@)([\w.-]+)(?::(\d+))?\/(.+)/;
Expand All @@ -12,8 +13,10 @@ const ERROR_MESSAGE = 'Invalid Dsn';
export class Dsn implements DsnComponents {
/** Protocol used to connect to Sentry. */
public protocol!: DsnProtocol;
/** Public authorization key. */
/** Public authorization key (deprecated, renamed to publicKey). */
public user!: string;
/** Public authorization key. */
public publicKey!: string;
/** Private authorization key (deprecated, optional). */
public pass!: string;
/** Hostname of the Sentry instance. */
Expand Down Expand Up @@ -46,9 +49,9 @@ export class Dsn implements DsnComponents {
* @param withPassword When set to true, the password will be included.
*/
public toString(withPassword: boolean = false): string {
const { host, path, pass, port, projectId, protocol, user } = this;
const { host, path, pass, port, projectId, protocol, publicKey } = this;
return (
`${protocol}://${user}${withPassword && pass ? `:${pass}` : ''}` +
`${protocol}://${publicKey}${withPassword && pass ? `:${pass}` : ''}` +
`@${host}${port ? `:${port}` : ''}/${path ? `${path}/` : path}${projectId}`
);
}
Expand All @@ -61,7 +64,7 @@ export class Dsn implements DsnComponents {
throw new SentryError(ERROR_MESSAGE);
}

const [protocol, user, pass = '', host, port = '', lastPath] = match.slice(1);
const [protocol, publicKey, pass = '', host, port = '', lastPath] = match.slice(1);
let path = '';
let projectId = lastPath;

Expand All @@ -78,13 +81,20 @@ export class Dsn implements DsnComponents {
}
}

this._fromComponents({ host, pass, path, projectId, port, protocol: protocol as DsnProtocol, user });
this._fromComponents({ host, pass, path, projectId, port, protocol: protocol as DsnProtocol, publicKey });
}

/** Maps Dsn components into this instance. */
private _fromComponents(components: DsnComponents): void {
if ('user' in components && !('publicKey' in components)) {
logger.warn('Use of `user` in DsnComponents is deprecated. Please use `publicKey` instead.');
components.publicKey = components.user;
}
// TODO this is for backwards compatibility, and can be removed in a future version
this.user = components.publicKey || '';

this.protocol = components.protocol;
this.user = components.user;
this.publicKey = components.publicKey || '';
this.pass = components.pass || '';
this.host = components.host;
this.port = components.port || '';
Expand All @@ -94,7 +104,7 @@ export class Dsn implements DsnComponents {

/** Validates this Dsn and throws on error. */
private _validate(): void {
['protocol', 'user', 'host', 'projectId'].forEach(component => {
['protocol', 'publicKey', 'host', 'projectId'].forEach(component => {
if (!this[component as keyof DsnComponents]) {
throw new SentryError(`${ERROR_MESSAGE}: ${component} missing`);
}
Expand Down
28 changes: 14 additions & 14 deletions packages/utils/test/dsn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ describe('Dsn', () => {
port: '1234',
projectId: '123',
protocol: 'https',
user: 'abc',
publicKey: 'abc',
});
expect(dsn.protocol).toBe('https');
expect(dsn.user).toBe('abc');
expect(dsn.publicKey).toBe('abc');
expect(dsn.pass).toBe('xyz');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('1234');
Expand All @@ -26,10 +26,10 @@ describe('Dsn', () => {
host: 'sentry.io',
projectId: '123',
protocol: 'https',
user: 'abc',
publicKey: 'abc',
});
expect(dsn.protocol).toBe('https');
expect(dsn.user).toBe('abc');
expect(dsn.publicKey).toBe('abc');
expect(dsn.pass).toBe('');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('');
Expand All @@ -44,7 +44,7 @@ describe('Dsn', () => {
host: '',
projectId: '123',
protocol: 'https',
user: 'abc',
publicKey: 'abc',
}),
).toThrow(SentryError);
expect(
Expand All @@ -53,7 +53,7 @@ describe('Dsn', () => {
host: 'sentry.io',
projectId: '',
protocol: 'https',
user: 'abc',
publicKey: 'abc',
}),
).toThrow(SentryError);
expect(
Expand All @@ -62,7 +62,7 @@ describe('Dsn', () => {
host: 'sentry.io',
projectId: '123',
protocol: '' as 'http', // Trick the type checker here
user: 'abc',
publicKey: 'abc',
}),
).toThrow(SentryError);
expect(
Expand All @@ -71,7 +71,7 @@ describe('Dsn', () => {
host: 'sentry.io',
projectId: '123',
protocol: 'https',
user: '',
publicKey: '',
}),
).toThrow(SentryError);
});
Expand All @@ -83,7 +83,7 @@ describe('Dsn', () => {
host: 'sentry.io',
projectId: '123',
protocol: 'httpx' as 'http', // Trick the type checker here
user: 'abc',
publicKey: 'abc',
}),
).toThrow(SentryError);
expect(
Expand All @@ -93,7 +93,7 @@ describe('Dsn', () => {
port: 'xxx',
projectId: '123',
protocol: 'https',
user: 'abc',
publicKey: 'abc',
}),
).toThrow(SentryError);
});
Expand All @@ -103,7 +103,7 @@ describe('Dsn', () => {
test('parses a valid full Dsn', () => {
const dsn = new Dsn('https://abc:xyz@sentry.io:1234/123');
expect(dsn.protocol).toBe('https');
expect(dsn.user).toBe('abc');
expect(dsn.publicKey).toBe('abc');
expect(dsn.pass).toBe('xyz');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('1234');
Expand All @@ -114,7 +114,7 @@ describe('Dsn', () => {
test('parses a valid partial Dsn', () => {
const dsn = new Dsn('https://abc@sentry.io/123/321');
expect(dsn.protocol).toBe('https');
expect(dsn.user).toBe('abc');
expect(dsn.publicKey).toBe('abc');
expect(dsn.pass).toBe('');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('');
Expand All @@ -125,7 +125,7 @@ describe('Dsn', () => {
test('with a long path', () => {
const dsn = new Dsn('https://abc@sentry.io/sentry/custom/installation/321');
expect(dsn.protocol).toBe('https');
expect(dsn.user).toBe('abc');
expect(dsn.publicKey).toBe('abc');
expect(dsn.pass).toBe('');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('');
Expand All @@ -136,7 +136,7 @@ describe('Dsn', () => {
test('with a query string', () => {
const dsn = new Dsn('https://abc@sentry.io/321?sample.rate=0.1&other=value');
expect(dsn.protocol).toBe('https');
expect(dsn.user).toBe('abc');
expect(dsn.publicKey).toBe('abc');
expect(dsn.pass).toBe('');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('');
Expand Down

0 comments on commit 3c62321

Please sign in to comment.