@usync/oauth2 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ export declare const OAUTH2_NEED_REFRESH = 1;
2
+ export declare const OAUTH2_AUTH_ERROR = 2;
3
+ export declare const OAUTH2_UNAUTHORIZED = 3;
4
+ export declare class OAuth2Error extends Error {
5
+ code: number;
6
+ constructor(code: number, message?: string);
7
+ }
@@ -0,0 +1,7 @@
1
+ import { OAuth2Authorizer } from "./providers";
2
+ import type { IOAuth2Account, IOAuth2Options } from "./types";
3
+ export * from "./common";
4
+ export * from "./providers";
5
+ export * from "./types";
6
+ export declare function getAuthorizer(options: IOAuth2Options, auth: IOAuth2Account): import("./providers").DropboxAuthorizer | import("./providers").GoogleAuthorizer | import("./providers").MicrosoftAuthorizer;
7
+ export declare function ensureAccessToken(authorizer: OAuth2Authorizer, handleOAuth2?: (url: string) => Promise<string>): Promise<string>;
@@ -0,0 +1,28 @@
1
+ import type { IOAuth2Options, TokenData } from "../types";
2
+ export declare abstract class OAuth2Authorizer {
3
+ protected options: IOAuth2Options;
4
+ abstract buildAuthUrl(): Promise<string>;
5
+ abstract finishAuth(url: URL): Promise<string>;
6
+ abstract refreshToken(): Promise<string>;
7
+ protected _accessToken: TokenData | null;
8
+ protected _refreshToken: TokenData | null;
9
+ session: {
10
+ state: string;
11
+ codeVerifier: string;
12
+ } | undefined;
13
+ constructor(options: IOAuth2Options, initialData?: {
14
+ accessToken?: TokenData;
15
+ refreshToken?: TokenData;
16
+ session?: {
17
+ state: string;
18
+ codeVerifier: string;
19
+ };
20
+ });
21
+ protected _getValidToken(value?: TokenData | null): string | undefined;
22
+ protected _updateAccessToken(value: TokenData | null): void;
23
+ protected _updateRefreshToken(value: TokenData | null): void;
24
+ getAccessToken(): string;
25
+ getRefreshToken(): string;
26
+ setAccessToken(value?: TokenData | null): void;
27
+ setRefreshToken(value?: TokenData | null): void;
28
+ }
@@ -0,0 +1,12 @@
1
+ import { OAuth2Authorizer } from "./base.ts";
2
+ export declare class DropboxAuthorizer extends OAuth2Authorizer {
3
+ /**
4
+ * Ref: https://www.dropbox.com/developers/documentation/http/documentation
5
+ */
6
+ static Scopes: {
7
+ account: string;
8
+ };
9
+ buildAuthUrl(): Promise<string>;
10
+ finishAuth(url: URL): Promise<string>;
11
+ refreshToken(): Promise<string>;
12
+ }
@@ -0,0 +1,17 @@
1
+ import { OAuth2Authorizer } from "./base.ts";
2
+ /**
3
+ * Ref: https://developers.google.com/identity/protocols/oauth2/web-server
4
+ */
5
+ export declare class GoogleAuthorizer extends OAuth2Authorizer {
6
+ /**
7
+ * Ref: https://developers.google.com/identity/protocols/oauth2/scopes
8
+ */
9
+ static Scopes: {
10
+ account: string;
11
+ "drive.appdata": string;
12
+ imap: string;
13
+ };
14
+ buildAuthUrl(): Promise<string>;
15
+ finishAuth(url: URL): Promise<string>;
16
+ refreshToken(): Promise<string>;
17
+ }
@@ -0,0 +1,10 @@
1
+ import { DropboxAuthorizer } from "./dropbox";
2
+ import { GoogleAuthorizer } from "./google";
3
+ import { MicrosoftAuthorizer } from "./microsoft";
4
+ export * from "./base";
5
+ export { DropboxAuthorizer, GoogleAuthorizer, MicrosoftAuthorizer };
6
+ export declare const OAuth2Authorizers: {
7
+ dropbox: typeof DropboxAuthorizer;
8
+ google: typeof GoogleAuthorizer;
9
+ microsoft: typeof MicrosoftAuthorizer;
10
+ };
@@ -0,0 +1,15 @@
1
+ import { OAuth2Authorizer } from "./base.ts";
2
+ /**
3
+ * Ref: https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-auth-code-flow
4
+ */
5
+ export declare class MicrosoftAuthorizer extends OAuth2Authorizer {
6
+ static Scopes: {
7
+ /** Ref: https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth */
8
+ imap: string;
9
+ onedrive: string;
10
+ };
11
+ private oauth2Url;
12
+ buildAuthUrl(): Promise<string>;
13
+ finishAuth(url: URL): Promise<string>;
14
+ refreshToken(): Promise<string>;
15
+ }
@@ -0,0 +1,30 @@
1
+ import { OAuth2Authorizers } from "./providers";
2
+ export interface TokenData {
3
+ token: string;
4
+ expiresAt?: number;
5
+ scope?: string;
6
+ }
7
+ export interface IOAuth2Options {
8
+ clientId: string;
9
+ /** clientSecret may be absent for client-side apps. */
10
+ clientSecret?: string;
11
+ redirectUrl: string;
12
+ scope?: string;
13
+ provider?: {
14
+ microsoft?: {
15
+ /**
16
+ * Must match the account type of the application registered in https://portal.azure.com/.
17
+ * `common` for all accounts, `consumers` for personal accounts only.
18
+ *
19
+ * Default as `common`.
20
+ */
21
+ accountType?: "common" | "consumers";
22
+ };
23
+ };
24
+ onSetAccessToken?: (value: TokenData | null) => void;
25
+ onSetRefreshToken?: (value: TokenData | null) => void;
26
+ }
27
+ export interface IOAuth2Account {
28
+ provider: keyof typeof OAuth2Authorizers;
29
+ user: string;
30
+ }
package/dist/util.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ export declare function getState(): string;
2
+ /**
3
+ * Ref: https://datatracker.ietf.org/doc/html/rfc7636#section-4.1
4
+ * high-entropy cryptographic random STRING using the
5
+ * unreserved characters [A-Z] / [a-z] / [0-9] / "-" / "." / "_" / "~"
6
+ * from Section 2.3 of [RFC3986], with a minimum length of 43 characters
7
+ * and a maximum length of 128 characters.
8
+ */
9
+ export declare function getCodeVerifier(): string;
10
+ export declare function getCodeChallenge(codeVerifier: string): Promise<{
11
+ codeChallenge: string;
12
+ codeChallengeMethod: string;
13
+ }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usync/oauth2",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "files": [
5
5
  "dist"
6
6
  ],
@@ -23,6 +23,6 @@
23
23
  "clean": "del-cli dist tsconfig.tsbuildinfo",
24
24
  "build:types": "tsc",
25
25
  "build:js": "vite build",
26
- "build": "pnpm clean && pnpm /^build:/"
26
+ "build": "pnpm clean && pnpm build:js && pnpm build:types"
27
27
  }
28
28
  }