Skip to content

Commit 5a04f80

Browse files
authored
feat(auth): add onTokenCreated option (#90)
* feat(auth): add onTokenCreated option * feat(auth): onTokenCreated is called for 'refresh' and 'reset' types * fix(auth): missing 'onTokenCreated' argument passed down when destructuring arguments * test(auth): add test coverage for 'onTokenCreated' option * refactor(types): move shared properties to a common type and re-use it * docs(readme): add information about onTokenCreated() option * feat(get-authentication): add 'onTokenCreated' when using 'getAuthentication()' * Update README.md
1 parent 96d3e46 commit 5a04f80

File tree

6 files changed

+271
-8
lines changed

6 files changed

+271
-8
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ The unguessable random string you provided in [Step 1 of GitHub's OAuth web appl
281281
<code>string</code>
282282
</th>
283283
<td>
284-
284+
285285
The <code>redirect_uri</code> parameter you provided in [Step 1 of GitHub's OAuth web application flow](https://docs.github.com/en/developers/apps/authorizing-oauth-apps#1-request-a-users-github-identity).
286286

287287
</td>
@@ -709,6 +709,17 @@ The differences are
709709
array of scope names enabled for the token
710710
</td>
711711
</tr>
712+
<tr>
713+
<th>
714+
<code>onTokenCreated</code>
715+
</th>
716+
<th>
717+
<code>function</code>
718+
</th>
719+
<td>
720+
callback invoked when a token is "reset" or "refreshed"
721+
</td>
722+
</tr>
712723
<tr>
713724
<th>
714725
<code>invalid</code>
@@ -808,6 +819,17 @@ Either `undefined` or `true`. Will be set to `true` if the token was invalided e
808819
The user access token
809820
</td>
810821
</tr>
822+
<tr>
823+
<th>
824+
<code>onTokenCreated</code>
825+
</th>
826+
<th>
827+
<code>function</code>
828+
</th>
829+
<td>
830+
callback invoked when a token is "reset" or "refreshed"
831+
</td>
832+
</tr>
811833
<tr>
812834
<th>
813835
<code>invalid</code>

src/auth.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ export async function auth(
8080
if (!currentAuthentication.hasOwnProperty("expiresAt")) {
8181
throw new Error("[@octokit/auth-oauth-user] Refresh token missing");
8282
}
83+
84+
await state.onTokenCreated?.(state.authentication, {
85+
type: options.type,
86+
});
8387
}
8488

8589
// check or reset token
@@ -102,6 +106,12 @@ export async function auth(
102106
...authentication,
103107
};
104108

109+
if (options.type === "reset") {
110+
await state.onTokenCreated?.(state.authentication, {
111+
type: options.type,
112+
});
113+
}
114+
105115
return state.authentication as
106116
| OAuthAppAuthentication
107117
| GitHubAppAuthentication

src/get-authentication.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export async function getAuthentication(
3232
clientId: state.clientId,
3333
clientSecret: state.clientSecret,
3434
clientType: state.clientType,
35+
onTokenCreated: state.onTokenCreated,
3536
...state.strategyOptions,
3637
request: state.request,
3738
});
@@ -48,6 +49,7 @@ export async function getAuthentication(
4849
const deviceAuth = createOAuthDeviceAuth({
4950
clientType: state.clientType,
5051
clientId: state.clientId,
52+
onTokenCreated: state.onTokenCreated,
5153
...state.strategyOptions,
5254
request: state.request,
5355
});
@@ -70,6 +72,7 @@ export async function getAuthentication(
7072
clientId: state.clientId,
7173
clientSecret: state.clientSecret,
7274
clientType: state.clientType,
75+
onTokenCreated: state.onTokenCreated,
7376
...state.strategyOptions,
7477
};
7578
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export function createOAuthUserAuth({
4747
"user-agent": `octokit-auth-oauth-app.js/${VERSION} ${getUserAgent()}`,
4848
},
4949
}),
50+
onTokenCreated,
5051
...strategyOptions
5152
}: OAuthAppStrategyOptions | GitHubAppStrategyOptions):
5253
| OAuthAppAuthInterface
@@ -55,6 +56,7 @@ export function createOAuthUserAuth({
5556
clientType,
5657
clientId,
5758
clientSecret,
59+
onTokenCreated,
5860
strategyOptions,
5961
request,
6062
});

src/types.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@ export type WebFlowOptions = {
1111
};
1212

1313
// STRATEGY OPTIONS
14-
15-
type CommonOAuthAppStrategyOptions = {
16-
clientType?: "oauth-app";
14+
type CommonAppStrategyOptions = {
15+
clientType?: ClientType;
1716
clientId: string;
1817
clientSecret: string;
1918
request?: OctokitTypes.RequestInterface;
19+
onTokenCreated?: OnToketCreatedCallback;
2020
};
21+
22+
type CommonOAuthAppStrategyOptions = {
23+
clientType?: "oauth-app";
24+
} & CommonAppStrategyOptions;
25+
2126
type CommonGitHubAppStrategyOptions = {
2227
clientType?: "github-app";
23-
clientId: string;
24-
clientSecret: string;
25-
request?: OctokitTypes.RequestInterface;
26-
};
28+
} & CommonAppStrategyOptions;
2729

2830
type OAuthAppDeviceFlowOptions = {
2931
onVerification: DeviceTypes.OAuthAppStrategyOptions["onVerification"];
@@ -119,11 +121,21 @@ export interface GitHubAppAuthInterface {
119121

120122
// INTERNAL STATE
121123

124+
type OnToketCreatedCallback = (
125+
authentication:
126+
| OAuthAppAuthentication
127+
| GitHubAppAuthentication
128+
| GitHubAppAuthenticationWithExpiration
129+
| undefined,
130+
options: OAuthAppAuthOptions | GitHubAppAuthOptions
131+
) => void | Promise<void>;
132+
122133
export type OAuthAppState = {
123134
clientId: string;
124135
clientSecret: string;
125136
clientType: "oauth-app";
126137
request: OctokitTypes.RequestInterface;
138+
onTokenCreated?: CommonAppStrategyOptions["onTokenCreated"];
127139
strategyOptions:
128140
| WebFlowOptions
129141
| OAuthAppDeviceFlowOptions
@@ -144,6 +156,7 @@ export type GitHubAppState = {
144156
clientSecret: string;
145157
clientType: "github-app";
146158
request: OctokitTypes.RequestInterface;
159+
onTokenCreated?: CommonAppStrategyOptions["onTokenCreated"];
147160
strategyOptions:
148161
| WebFlowOptions
149162
| GitHubDeviceFlowOptions

0 commit comments

Comments
 (0)