Skip to content

Commit 1f6b3ca

Browse files
committed
feat: Support grant_type: authorization_code and client_credentials.
1 parent fce7a60 commit 1f6b3ca

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

src/mastodon/oauth/token-repository.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
11
import { type HttpMetaParams } from "../../interfaces";
22
import { type Token } from "../entities/v1";
33

4-
export interface CreateTokenParamsWithPassword {
5-
readonly grantType: "password";
4+
/**
5+
* @deprecated Use `CreateTokenParamsWithPassword` instead
6+
*/
7+
export type CreateTokenParamsWithPassword = CreateTokenWithPasswordParams;
8+
9+
interface BaseCreateTokenParams<T extends string> {
10+
/** Set equal to `authorization_code` if code is provided in order to gain user-level access. Otherwise, set equal to `client_credentials` to obtain app-level access only. */
11+
readonly grantType: T;
12+
/** The client ID, obtained during app registration. */
613
readonly clientId: string;
14+
/** The client secret, obtained during app registration. */
715
readonly clientSecret: string;
8-
readonly username: string;
9-
readonly password: string;
16+
/** Set a URI to redirect the user to. If this parameter is set to urn:ietf:wg:oauth:2.0:oob then the token will be shown instead. Must match one of the `redirect_uris` declared during app registration. */
17+
readonly redirectUri: string;
18+
/** List of requested OAuth scopes, separated by spaces (or by pluses, if using query parameters). If code was provided, then this must be equal to the `scope` requested from the user. Otherwise, it must be a subset of `scopes` declared during app registration. If not provided, defaults to read. */
1019
readonly scope?: string;
1120
}
1221

13-
export type CreateTokenParams = CreateTokenParamsWithPassword;
22+
export interface CreateTokenWithAuthorizationCodeParams
23+
extends BaseCreateTokenParams<"authorization_code"> {
24+
/** A user authorization code, obtained via GET /oauth/authorize. */
25+
readonly code: string;
26+
}
27+
28+
export type CreateTokenWithClientCredentialsParams =
29+
BaseCreateTokenParams<"client_credentials">;
30+
31+
export interface CreateTokenWithPasswordParams
32+
extends BaseCreateTokenParams<"password"> {
33+
readonly password: string;
34+
readonly username: string;
35+
}
36+
37+
export type CreateTokenParams =
38+
| CreateTokenWithClientCredentialsParams
39+
| CreateTokenWithPasswordParams
40+
| CreateTokenWithAuthorizationCodeParams;
1441

1542
export interface TokenRepository {
1643
create(

test-utils/jest-global-setup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const readOrCreateAdminToken = async (
5151
username: "admin@localhost",
5252
password: "mastodonadmin",
5353
scope: "read write follow push admin:read admin:write",
54+
redirectUri: "urn:ietf:wg:oauth:2.0:oob",
5455
});
5556

5657
fs.writeFile(tokenFilePath, JSON.stringify(token, undefined, 2));

test-utils/pools/token-factory-docker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export class TokenFactoryDocker implements TokenFactory {
3838
username: email,
3939
password,
4040
scope: "read write follow push admin:read admin:write",
41+
redirectUri: "urn:ietf:wg:oauth:2.0:oob",
4142
});
4243

4344
return token;

tests/oauth/token.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ it("issues and revokes token", async () => {
1313
username: "admin@localhost",
1414
password: "mastodonadmin",
1515
scope: "read",
16+
redirectUri: "urn:ietf:wg:oauth:2.0:oob",
1617
});
1718

1819
expect(token).toHaveProperty("accessToken");

0 commit comments

Comments
 (0)