Skip to content

Commit a612232

Browse files
authored
🔥 feat: Remove password grant_type (#159)
* 🔥 feat: Remove password grant_type * 📝 feat: Update docs
1 parent 6276119 commit a612232

File tree

5 files changed

+30
-150
lines changed

5 files changed

+30
-150
lines changed

packages/drupal-auth-client/README.md

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,16 @@ Make sure your Drupal site is using the [simple_oauth](https://www.drupal.org/pr
99
### Importing library
1010

1111
```javascript
12-
import { drupalAuthClient } from "drupal-auth-client"
12+
import { drupalAuthClient } from "drupal-auth-client";
1313
```
1414

1515
### Using `client_credentials`
1616

1717
```javascript
18-
19-
const client = drupalAuthClient(
20-
"https://drupal.site",
21-
"client_credentials",
22-
{
23-
clientId: "client_id",
24-
clientSecret: "client_secret",
25-
},
26-
)
27-
```
28-
29-
### Using `password`
30-
31-
```javascript
32-
33-
const client = drupalAuthClient(
34-
"https://drupal.site",
35-
"password",
36-
{
37-
username: "username",
38-
password: "password",
39-
clientId: "client_id",
40-
clientSecret: "client_secret",
41-
},
42-
)
18+
const client = drupalAuthClient("https://drupal.site", {
19+
clientId: "client_id",
20+
clientSecret: "client_secret",
21+
});
4322
```
4423

4524
## Supporting organizations

packages/drupal-auth-client/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
{
22
"name": "drupal-auth-client",
3-
"version": "0.4.0",
3+
"version": "1.0.0",
44
"description": "Drupal Auth Client",
55
"keywords": [
66
"auth",
77
"drupal"
88
],
99
"main": "dist/index.js",
1010
"files": [
11-
"dist",
12-
"src"
11+
"dist"
1312
],
1413
"bundlesize": [
1514
{
@@ -29,4 +28,4 @@
2928
"@types/node": "^20.12.12",
3029
"typescript": "^5.4.5"
3130
}
32-
}
31+
}

packages/drupal-auth-client/src/authHandlers.ts

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const clientCredentialsHeader = async (
44
authUri: string,
55
clientId: string,
66
clientSecret: string,
7-
fetcher: Config["fetcher"],
7+
fetcher: Config["fetcher"]
88
) => {
99
const formData = new FormData();
1010
formData.append("grant_type", "client_credentials");
@@ -20,37 +20,7 @@ export const clientCredentialsHeader = async (
2020
});
2121

2222
if (response.ok) {
23-
return await response.json() as OAuthPayload;
24-
}
25-
26-
return null;
27-
};
28-
29-
export const passwordHeader = async (
30-
authUri: string,
31-
username: string,
32-
password: string,
33-
clientId: string,
34-
clientSecret: string,
35-
fetcher: Config["fetcher"],
36-
) => {
37-
const formData = new FormData();
38-
formData.append("grant_type", "password");
39-
formData.append("client_id", clientId);
40-
formData.append("client_secret", clientSecret);
41-
formData.append("username", username);
42-
formData.append("password", password);
43-
44-
const response = await fetcher(authUri, {
45-
method: "post",
46-
headers: {
47-
Accept: "application/json",
48-
},
49-
body: formData,
50-
});
51-
52-
if (response.ok) {
53-
return await response.json() as OAuthPayload;
23+
return (await response.json()) as OAuthPayload;
5424
}
5525

5626
return null;
Lines changed: 16 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,43 @@
1-
import { clientCredentialsHeader, passwordHeader } from "./authHandlers";
2-
import type { Auth, Config, Options } from "./types";
1+
import { clientCredentialsHeader } from "./authHandlers";
2+
import type { Config, Options } from "./types";
33

4-
const calculateAuthHeader = async <TAuth extends Auth["token_type"]>(
4+
const calculateAuthHeader = async (
55
uri: string,
6-
type: TAuth,
7-
options: Options<TAuth>,
6+
options: Options,
87
fetcher: Config["fetcher"]
98
) => {
10-
if (type === "client_credentials") {
11-
const { clientId, clientSecret} =
12-
options as Options<"client_credentials">;
13-
const header = await clientCredentialsHeader(
14-
uri,
15-
clientId,
16-
clientSecret,
17-
fetcher,
18-
);
19-
20-
if (header) {
21-
return header;
22-
}
23-
}
24-
25-
if (type === "password") {
26-
const { username, password, clientId, clientSecret } =
27-
options as Options<"password">;
28-
const header = await passwordHeader(
29-
uri,
30-
username,
31-
password,
32-
clientId,
33-
clientSecret,
34-
fetcher,
35-
);
9+
const { clientId, clientSecret } = options;
10+
const header = await clientCredentialsHeader(
11+
uri,
12+
clientId,
13+
clientSecret,
14+
fetcher
15+
);
3616

37-
if (header) {
38-
return header;
39-
}
17+
if (header) {
18+
return header;
4019
}
41-
42-
return null;
4320
};
4421

4522
/**
4623
* Return headers based on the given auth and config
4724
* @param uri The uri of the drupal site
48-
* @param auth The auth strategy to use
4925
* @param options {Config} The auth options
5026
* @param config The config for the client
5127
*
5228
* The type for the options is decided by the auth type
5329
* @example
5430
* const client = drupalAuthClient(
5531
* "https://drupal.site",
56-
* "client_credentials",
5732
* {
5833
* clientId: "client_id",
5934
* clientSecret: "client_secret",
6035
* },
6136
* );
62-
*
63-
* In the above example, you only need to provide the clientId and clientSecret
64-
* because the auth type is "client_credentials" but if you set the auth type to
65-
* "password" you would need to provide the username and password as well.
66-
*
67-
* @example
68-
* const client = drupalAuthClient(
69-
* "https://drupal.site",
70-
* "password",
71-
* {
72-
* username: "username",
73-
* password: "password",
74-
* clientId: "client_id",
75-
* clientSecret: "client_secret",
76-
* }
77-
* );
78-
*
79-
*
8037
**/
81-
const drupalAuthClient = async <TAuth extends Auth["token_type"]>(
38+
const drupalAuthClient = async (
8239
uri: string,
83-
type: TAuth,
84-
options: Options<TAuth>,
40+
options: Options,
8541
config: Config = {
8642
fetcher: fetch,
8743
}
@@ -91,12 +47,7 @@ const drupalAuthClient = async <TAuth extends Auth["token_type"]>(
9147
const url = new URL(uri);
9248
const formattedAuthURI = authURI ? authURI : `${url.origin}/oauth/token`;
9349

94-
const header = await calculateAuthHeader(
95-
formattedAuthURI,
96-
type,
97-
options,
98-
fetcher
99-
)
50+
const header = await calculateAuthHeader(formattedAuthURI, options, fetcher);
10051

10152
if (!header) {
10253
throw new Error("Unable to fetch auth header");
@@ -106,8 +57,3 @@ const drupalAuthClient = async <TAuth extends Auth["token_type"]>(
10657
};
10758

10859
export { drupalAuthClient };
109-
110-
/**
111-
* @deprecated
112-
*/
113-
export default drupalAuthClient;

packages/drupal-auth-client/src/types.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
1-
type ClientOptions = {
1+
export type Options = {
22
clientId: string;
33
clientSecret: string;
44
};
55

66
type ClientCredentialsOAuth = {
7-
token_type: "client_credentials";
8-
options: ClientOptions;
7+
options: Options;
98
};
109

11-
type PasswordOAuth = {
12-
token_type: "password";
13-
options: {
14-
username: string;
15-
password: string;
16-
} & ClientOptions;
17-
};
18-
19-
export type Auth = ClientCredentialsOAuth | PasswordOAuth;
20-
21-
export type Options<TConfig extends Auth["token_type"]> = Extract<
22-
Auth,
23-
{ token_type: TConfig }
24-
>["options"];
10+
export type Auth = ClientCredentialsOAuth;
2511

2612
export type Config = {
27-
fetcher: any;
13+
fetcher: typeof fetch;
2814
authURI?: string;
2915
};
3016

0 commit comments

Comments
 (0)