Skip to content

Commit

Permalink
feat: allow application-only-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed Mar 12, 2021
1 parent 93d60e8 commit 487c5fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
5 changes: 2 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ export default class Client {
}

protected async updateAccessToken(): Promise<void> {
if (!this.auth) throw "No auth";
if (!this.creds) throw "No creds";

this.token = await refreshToken(
this.userAgent,
this.token,
this.auth,
this.creds,
this.userAgent
this.auth
);
}

Expand Down
20 changes: 12 additions & 8 deletions src/helper/accessToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,27 @@ export interface TokenResponse {
}

export default async function updateAccessToken(
userAgent: string,
token: Token | null,
auth: Auth,
creds: Credentials,
userAgent: string
auth?: Auth
): Promise<Token> {
const expiration = token?.expiration ?? 0;
if (expiration > Date.now()) return token!;

// Token is expired or missing, time to (re)generate!
let grant: Data = {};
if ("refreshToken" in auth) {
grant.grant_type = "refresh_token";
grant.refresh_token = auth.refreshToken;
if (auth) {
if ("refreshToken" in auth) {
grant.grant_type = "refresh_token";
grant.refresh_token = auth.refreshToken;
} else {
grant.grant_type = "password";
grant.username = auth.username;
grant.password = auth.password;
}
} else {
grant.grant_type = "password";
grant.username = auth.username;
grant.password = auth.password;
grant.grant_type = "client_credentials";
}

const raw: Data = await postForm(
Expand Down

0 comments on commit 487c5fd

Please sign in to comment.