Skip to content

Commit

Permalink
added params option
Browse files Browse the repository at this point in the history
  • Loading branch information
flexwie committed Dec 19, 2021
1 parent fcad57e commit 417f7ee
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 32 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"axios": "^0.24.0"
},
"devDependencies": {
"@types/node": "^16.11.6",
"@types/node": "^16.11.14",
"rimraf": "^3.0.2"
},
"types": "dist/index.d.ts",
Expand Down
70 changes: 52 additions & 18 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
import axios, { AxiosResponse } from "axios";
import { ResponseError } from "./error";
import {
CacheEntry,
Expand All @@ -16,7 +16,7 @@ export class DeviceFlowClient {
constructor(
public connection: DeviceFlowDetails,
public options?: ClientOptions
) {}
) { }

private log(str: string) {
if (this.options?.output) {
Expand Down Expand Up @@ -52,15 +52,31 @@ export class DeviceFlowClient {
? [...this.connection.scopes, "offline_access"]
: this.connection.scopes;

// get device and user code from remote
const response = await axios.post<DeviceCodeResponse>(
this.connection.code_url,
{
client_id: this.connection.client_id,
scope: scopes.join(" "),
audience: this.connection.audience,
}
);
let response

if (this.options?.useParams) {
const params = new URLSearchParams()
params.append('client_id', this.connection.client_id)
params.append('audience', this.connection.audience)
params.append('scope', scopes.join(" "))

response = await axios.post<DeviceCodeResponse>(this.connection.code_url, params, {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
})
} else {
// get device and user code from remote
response = await axios.post<DeviceCodeResponse>(
this.connection.code_url,
{
client_id: this.connection.client_id,
scope: scopes.join(" "),
audience: this.connection.audience,
}
);

}

return response.data;
} catch (error: any) {
Expand All @@ -81,14 +97,32 @@ export class DeviceFlowClient {
device_code: string
): Promise<TokenResponseSuccess | null> {
try {
let response: AxiosResponse

// request the access token
const response = await axios.post<
Partial<TokenResponseError & TokenResponseSuccess>
>(this.connection.token_url, {
client_id: this.connection.client_id,
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
device_code,
});
if (this.options?.useParams) {
const params = new URLSearchParams()
params.append('client_id', this.connection.client_id)
params.append('device_code', device_code)
params.append('grant_type', "urn:ietf:params:oauth:grant-type:device_code")

response = await axios.post<
Partial<TokenResponseError & TokenResponseSuccess>
>(this.connection.token_url, params, {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
});

} else {
response = await axios.post<
Partial<TokenResponseError & TokenResponseSuccess>
>(this.connection.token_url, {
client_id: this.connection.client_id,
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
device_code,
});
}

// return tokens if successfull
if (response.data.access_token) {
Expand Down
12 changes: 8 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export type ClientOptions = {
* Request a refresh token?
*/
refreshToken?: boolean;
/**
* Weather too send data as params in requests
*/
useParams?: boolean
};

export type CacheEntry = TokenResponseSuccess & {
Expand All @@ -72,10 +76,10 @@ export type DeviceCodeResponse = {

export type TokenResponseError = {
error:
| "authorization_pending"
| "slow_down"
| "expired_token"
| "access_denied";
| "authorization_pending"
| "slow_down"
| "expired_token"
| "access_denied";
error_description: string;
};

Expand Down

0 comments on commit 417f7ee

Please sign in to comment.