Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/platforms/Bluesky/Bluesky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class Bluesky extends Platform {
return await this.test();
}
throw this.user.log.error(
`Platform.setup: ui ${operator.ui} not supported`,
`${this.id} setup: ui ${operator.ui} not supported`,
);
}

Expand Down
11 changes: 8 additions & 3 deletions src/platforms/Facebook/Facebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ export default class Facebook extends Platform {
await this.auth.setupCli();
return await this.test();
}
if (!payload) {
throw this.user.log.error("Setup via api requires a payload");
if (operator.ui === "api") {
if (!payload) {
throw this.user.log.error("Setup via api requires a payload");
}
return this.auth.setupApi(payload);
}
return this.auth.setupApi(payload);
throw this.user.log.error(
`${this.id} setup: ui ${operator.ui} not supported`,
);
}

/** @inheritdoc */
Expand Down
11 changes: 8 additions & 3 deletions src/platforms/Instagram/Instagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,15 @@ export default class Instagram extends Platform {
await this.auth.setupCli();
return await this.test();
}
if (!payload) {
throw this.user.log.error("Setup via api requires a payload");
if (operator.ui === "api") {
if (!payload) {
throw this.user.log.error("Setup via api requires a payload");
}
return this.auth.setupApi(payload);
}
return this.auth.setupApi(payload);
throw this.user.log.error(
`${this.id} setup: ui ${operator.ui} not supported`,
);
}

/** @inheritdoc */
Expand Down
18 changes: 15 additions & 3 deletions src/platforms/LinkedIn/LinkedIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,22 @@ export default class LinkedIn extends Platform {
await this.auth.setupCli();
return await this.test();
}
if (!payload) {
throw this.user.log.error("Setup via api requires a payload");
if (operator.ui === "api") {
if (!payload) {
throw this.user.log.error("Setup via api requires a payload");
}
const result = await this.auth.setupApi(payload);
const ready = "ready" in result && result.ready;
if (!ready) return result;
return {
...result,
test: await this.test(),
};
}
return this.auth.setupApi(payload);

throw this.user.log.error(
`${this.id} setup: ui ${operator.ui} not supported`,
);
}

/** @inheritdoc */
Expand Down
118 changes: 75 additions & 43 deletions src/platforms/LinkedIn/LinkedInAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,54 +21,55 @@ export default class LinkedInAuth {
* Set up LinkedIn platform
*/
async setupCli() {
const code = await this.requestCode();
const tokens = await this.exchangeCode(code);
const clientHost = this.user.data.get("app", "OAUTH_HOSTNAME");
const clientPort = Number(this.user.data.get("app", "OAUTH_PORT"));
const redirectUri = OAuth2Service.getCallbackUrl(clientHost, clientPort);
const code = await this.requestCliCode(redirectUri);
const tokens = await this.exchangeCode(code, redirectUri);
await this.store(tokens);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async setupApi(payload: object) {
throw this.user.log.error("LinkedInAuth:setupApi - not implemented");
}

/**
* Refresh LinkedIn tokens
*/
async refresh() {
const tokens = (await this.post("accessToken", {
grant_type: "refresh_token",
refresh_token: this.user.data.get("auth", "LINKEDIN_REFRESH_TOKEN"),
client_id: this.user.data.get("app", "LINKEDIN_CLIENT_ID"),
client_secret: this.user.data.get("app", "LINKEDIN_CLIENT_SECRET"),
})) as TokenResponse;

if (!isTokenResponse(tokens)) {
throw this.user.log.error(
"LinkedInAuth.refresh: response is not a TokenResponse",
tokens,
);
async setupApi(payload: {
state?: string;
redirect_uri?: string;
code?: string;
error?: string;
error_uri?: string;
error_description?: string;
}): Promise<{ url?: string; ready?: boolean }> {
if (payload["error"]) {
const msg = payload["error"] + " - " + payload["error_description"];
throw this.user.log.error(msg, payload);
}
if (!payload.redirect_uri) {
throw this.user.log.error("LinkedInAuth.setup: Invalid payload", payload);
}
if (!payload.code) {
return {
url: this.getRequestUrl(payload.redirect_uri, payload.state),
};
}
const tokens = await this.exchangeCode(payload.code, payload.redirect_uri);
await this.store(tokens);
return {
ready: true,
};
}

/**
* Request remote code using OAuth2Service
* @returns - code
* Get oath2 url to request a code
* @param redirectUri
* @param state
* @returns - string
*/
private async requestCode(): Promise<string> {
this.user.log.trace("LinkedInAuth", "requestCode");
private getRequestUrl(redirectUri: string, state?: string): string {
const clientId = this.user.data.get("app", "LINKEDIN_CLIENT_ID");
const clientHost = this.user.data.get("app", "OAUTH_HOSTNAME");
const clientPort = Number(this.user.data.get("app", "OAUTH_PORT"));
const state = String(Math.random()).substring(2);

// create auth url
const url = new URL("https://www.linkedin.com");
url.pathname = "oauth/" + this.API_VERSION + "/authorization";
const query = {
client_id: clientId,
redirect_uri: OAuth2Service.getCallbackUrl(clientHost, clientPort),
state: state,
redirect_uri: redirectUri,
...(state ? { state: state } : {}),
response_type: "code",
duration: "permanent",
scope: [
Expand All @@ -78,12 +79,23 @@ export default class LinkedInAuth {
].join(" "),
};
url.search = new URLSearchParams(query).toString();
return url.href;
}

/**
* Request remote code using OAuth2Service as a local server
* @param redirectUri
* @returns - code
*/
private async requestCliCode(redirectUri: string): Promise<string> {
this.user.log.trace("LinkedInAuth", "requestCode");
const state = String(Math.random()).substring(2);
const requestUrl = this.getRequestUrl(redirectUri, state);
const result = await OAuth2Service.requestRemotePermissions(
"LinkedIn",
url.href,
clientHost,
clientPort,
requestUrl,
this.user.data.get("app", "OAUTH_HOSTNAME"),
Number(this.user.data.get("app", "OAUTH_PORT")),
);
if (result["error"]) {
const msg = result["error_reason"] + " - " + result["error_description"];
Expand All @@ -103,14 +115,14 @@ export default class LinkedInAuth {
/**
* Exchange remote code for tokens
* @param code - the code to exchange
* @param redirectUri
* @returns - TokenResponse
*/
private async exchangeCode(code: string): Promise<TokenResponse> {
this.user.log.trace("LinkedInAuth", "exchangeCode", code);
const clientHost = this.user.data.get("app", "OAUTH_HOSTNAME");
const clientPort = Number(this.user.data.get("app", "OAUTH_PORT"));
const redirectUri = OAuth2Service.getCallbackUrl(clientHost, clientPort);

private async exchangeCode(
code: string,
redirectUri: string,
): Promise<TokenResponse> {
this.user.log.trace("LinkedInAuth", "exchangeCode");
const tokens = (await this.post("accessToken", {
grant_type: "authorization_code",
code: code,
Expand All @@ -126,6 +138,26 @@ export default class LinkedInAuth {
return tokens;
}

/**
* Refresh LinkedIn tokens
*/
async refresh() {
const tokens = (await this.post("accessToken", {
grant_type: "refresh_token",
refresh_token: this.user.data.get("auth", "LINKEDIN_REFRESH_TOKEN"),
client_id: this.user.data.get("app", "LINKEDIN_CLIENT_ID"),
client_secret: this.user.data.get("app", "LINKEDIN_CLIENT_SECRET"),
})) as TokenResponse;

if (!isTokenResponse(tokens)) {
throw this.user.log.error(
"LinkedInAuth.refresh: response is not a TokenResponse",
tokens,
);
}
await this.store(tokens);
}

/**
* Save all tokens in auth store
* @param tokens - the tokens to store
Expand Down
11 changes: 8 additions & 3 deletions src/platforms/Reddit/Reddit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ export default class Reddit extends Platform {
await this.auth.setupCli();
return await this.test();
}
if (!payload) {
throw this.user.log.error("Setup via api requires a payload");
if (operator.ui === "api") {
if (!payload) {
throw this.user.log.error("Setup via api requires a payload");
}
return this.auth.setupApi(payload);
}
return this.auth.setupApi(payload);
throw this.user.log.error(
`${this.id} setup: ui ${operator.ui} not supported`,
);
}

/** @inheritdoc */
Expand Down
11 changes: 8 additions & 3 deletions src/platforms/Twitter/Twitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ export default class Twitter extends Platform {
await this.auth.setupCli();
return await this.test();
}
if (!payload) {
throw this.user.log.error("Setup via api requires a payload");
if (operator.ui === "api") {
if (!payload) {
throw this.user.log.error("Setup via api requires a payload");
}
return this.auth.setupApi(payload);
}
return this.auth.setupApi(payload);
throw this.user.log.error(
`${this.id} setup: ui ${operator.ui} not supported`,
);
}

/** @inheritdoc */
Expand Down
11 changes: 8 additions & 3 deletions src/platforms/YouTube/YouTube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ export default class YouTube extends Platform {
await this.auth.setupCli();
return await this.test();
}
if (!payload) {
throw this.user.log.error("Setup via api requires a payload");
if (operator.ui === "api") {
if (!payload) {
throw this.user.log.error("Setup via api requires a payload");
}
return this.auth.setupApi(payload);
}
return this.auth.setupApi(payload);
throw this.user.log.error(
`${this.id} setup: ui ${operator.ui} not supported`,
);
}

/** @inheritdoc */
Expand Down