Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit f37bac5

Browse files
committed
feat(login): Remove apiKey login support.
1 parent d60d71d commit f37bac5

File tree

6 files changed

+19
-51
lines changed

6 files changed

+19
-51
lines changed

src/Session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export default class Session extends EventEmitter {
6868
this.options.unauthenticated = true
6969
}
7070
this.loginFunction = async () => {
71-
throw new Error('Need either "privateKey", "ethereum", "apiKey", "username"+"password" or "sessionToken" to login.')
71+
throw new Error('Need either "privateKey", "ethereum" or "sessionToken" to login.')
7272
}
7373
}
7474
}

src/rest/LoginEndpoints.ts

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,38 +77,17 @@ export class LoginEndpoints {
7777
}
7878

7979
/** @internal */
80-
async loginWithApiKey(apiKey: string) {
81-
this.client.debug('loginWithApiKey %o', {
82-
apiKey,
83-
})
84-
const url = getEndpointUrl(this.client.options.restUrl, 'login', 'apikey')
85-
const props = {
86-
apiKey,
87-
}
88-
return getSessionToken(url, props)
80+
// eslint-disable-next-line @typescript-eslint/no-unused-vars, class-methods-use-this
81+
async loginWithApiKey(_apiKey: string): Promise<any> {
82+
const message = 'apiKey auth is no longer supported. Please create an ethereum identity.'
83+
throw new AuthFetchError(message)
8984
}
9085

9186
/** @internal */
92-
async loginWithUsernamePassword(username: string, password: string) {
93-
this.client.debug('loginWithUsernamePassword %o', {
94-
username,
95-
})
96-
const url = getEndpointUrl(this.client.options.restUrl, 'login', 'password')
97-
const props = {
98-
username,
99-
password,
100-
}
101-
try {
102-
return await getSessionToken(url, props)
103-
} catch (err) {
104-
if (err && err.response && err.response.status === 404) {
105-
// this 404s if running against new backend with username/password support removed
106-
// wrap with appropriate error message
107-
const message = 'username/password auth is no longer supported. Please create an ethereum identity.'
108-
throw new AuthFetchError(message, err.response, err.body)
109-
}
110-
throw err
111-
}
87+
// eslint-disable-next-line @typescript-eslint/no-unused-vars, class-methods-use-this
88+
async loginWithUsernamePassword(_username: string, _password: string): Promise<any> {
89+
const message = 'username/password auth is no longer supported. Please create an ethereum identity.'
90+
throw new AuthFetchError(message)
11291
}
11392

11493
async getUserInfo() {

src/user/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ async function getUsername(client: StreamrClient) {
1313
return (
1414
username
1515
// edge case: if auth.apiKey is an anonymous key, userInfo.id is that anonymous key
16+
// update: not sure if still needed now that apiKey auth has been disabled
1617
|| id
1718
)
1819
}
@@ -48,5 +49,5 @@ export async function getUserId(client: StreamrClient) {
4849
return sha256(hexString)
4950
}
5051

51-
throw new Error('Need either "privateKey", "ethereum", "apiKey", "username"+"password" or "sessionToken" to derive the publisher Id.')
52+
throw new Error('Need either "privateKey", "ethereum" or "sessionToken" to derive the publisher Id.')
5253
}

test/integration/LoginEndpoints.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ describe('LoginEndpoints', () => {
8383
const sessionToken = await client.loginWithApiKey('tester1-api-key')
8484
assert(sessionToken)
8585
assert(sessionToken.token)
86-
// @ts-expect-error
8786
assert(sessionToken.expires)
8887
})
8988
})

test/integration/Session.test.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,13 @@ describe('Session', () => {
1212
})
1313

1414
describe('Token retrievals', () => {
15-
it('gets the token using api key', async () => {
15+
it('fails if trying to use apiKey', async () => {
1616
expect.assertions(1)
17-
await expect(createClient({
17+
await expect(() => createClient({
1818
auth: {
1919
apiKey: 'tester1-api-key',
2020
},
21-
}).session.getSessionToken()).resolves.toBeTruthy()
22-
})
23-
24-
it('fails when the used api key is invalid', async () => {
25-
expect.assertions(1)
26-
await expect(createClient({
27-
auth: {
28-
apiKey: 'wrong-api-key',
29-
},
30-
}).session.getSessionToken()).rejects.toMatchObject({
31-
body: expect.stringMatching(/invalid api key/i),
32-
})
21+
}).session.getSessionToken()).rejects.toThrow('no longer supported')
3322
})
3423

3524
it('gets the token using private key', async () => {

test/unit/Session.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('Session', () => {
6464
await expect(async () => (
6565
clientSessionToken.session.loginFunction()
6666
)).rejects.toThrow(
67-
'Need either "privateKey", "ethereum", "apiKey", "username"+"password" or "sessionToken" to login.'
67+
'Need either "privateKey", "ethereum" or "sessionToken" to login.'
6868
)
6969
})
7070

@@ -74,16 +74,16 @@ describe('Session', () => {
7474
})
7575
clientNone.onError = () => {}
7676
await clientNone.session.loginFunction().catch((err) => {
77-
expect(err.toString()).toEqual(
78-
'Error: Need either "privateKey", "ethereum", "apiKey", "username"+"password" or "sessionToken" to login.'
77+
expect(err.message).toEqual(
78+
'Need either "privateKey", "ethereum" or "sessionToken" to login.'
7979
)
8080
})
8181
clientNone.onError = () => {}
8282

8383
await expect(async () => (
8484
clientSessionToken.session.loginFunction()
8585
)).rejects.toThrow(
86-
'Need either "privateKey", "ethereum", "apiKey", "username"+"password" or "sessionToken" to login.'
86+
'Need either "privateKey", "ethereum" or "sessionToken" to login.'
8787
)
8888
})
8989
})
@@ -134,7 +134,7 @@ describe('Session', () => {
134134
beforeEach(() => {
135135
session = new Session(undefined as any)
136136
session.options.unauthenticated = false
137-
msg = 'Error: Need either "privateKey", "ethereum", "apiKey" or "username"+"password" to login.'
137+
msg = 'Need either "privateKey", "ethereum" or "sessionToken" to login.'
138138
session.loginFunction = sinon.stub().rejects(new Error(msg))
139139
clientSessionToken.onError = () => {}
140140
})

0 commit comments

Comments
 (0)