Skip to content

Conversation

@elien2016
Copy link
Collaborator

@elien2016 elien2016 commented Jan 11, 2024

Describe your changes

  • added basic auth and Mixpanel integration
  • added some types and narrowed param or return types at some places
  • added try catch in ExternalAuthIntegrationService

(haven't encrypted yet, will do later)
(will add/update docs and tests after a preliminary review)

Issue ticket number and link

FI-49
https://catting.atlassian.net/browse/FI-49?atlOrigin=eyJpIjoiNDk0ZDhiOGQ2ZGQyNDc2MjlhZDA1MWQwMGQ3YTUxYmYiLCJwIjoiaiJ9

Checklist before requesting a review

  • I have performed a self-review of my code
  • If it is a core feature, I have added thorough tests.
  • Do we need to implement analytics?
  • Will this be part of a product update? If yes, please write one phrase about this update.

@elien2016 elien2016 added the enhancement New feature or request label Jan 11, 2024
@codesandbox
Copy link

codesandbox bot commented Jan 11, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to change comments

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of this draft PR is to check if the general idea of the implementation of basic auth is correct; all the comments and docs were pasted over from existing files and are all wrong and should be ignored. I'll add docs and tests and linting and formatting after getting confirmation that the general idea is right...

export type BasicAuthString = `basic-${string}`;

/**
* Represents the client credential for Basic HTTP Authentication.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that in OAuth2, ICredentials may not a good name, it should be like IClientCredentials or sth

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is IClientCredentials meant to be shared with other types of auth, like Basic Auth?

* @property {number} [tokenRefreshBuffer] - Time in seconds before expiration to refresh the token.
* @property {{ [key: string]: string }} [callbackUriParams] - Optional additional parameters for the callback URI.
*/
export interface BasicAuthProvider {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IBasicAuthConfiguration

* @property {string} callbackUri - The URI for the OAuth2 callback after authentication.
* @property {{ [key: string]: string }} [callbackUriParams] - Optional additional parameters for the callback URI.
*/
export interface BasicAuthConfig {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that name in OAuth may be confused, actually BasicAuthConfig cannot store pw and username

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pw and username are the service account username and secret; I thought that's the credential we're supposed to put in .env (like the clientId and client secret of OAuth)? The authenticate() method basically just verifies it, and the verify() method retrieves an expiration date (generally service accounts don't expire so it would be null most of the time) and some other info. We should discuss it tmr, I'm a bit confused lol

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will store PW and username in db, actually in OAuth 2, we still store credentials in db, however the interface name ICredential in OAuth is confused, I have made a new PR to change it

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change comment

authenticateUrl: "https://mixpanel.com/api/app/me",
verifyUrl: "https://mixpanel.com/api/app/organizations/{organizationId}/service-accounts",
},
credential: { username: process.env.MIXPANEL_V1_BASIC_USERNAME, password: process.env.MIXPANEL_V1_BASIC_PASSWORD },
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rm credential: { username: process.env.MIXPANEL_V1_BASIC_USERNAME, password: process.env.MIXPANEL_V1_BASIC_PASSWORD }, as previous described

* @param _accessToken
* @returns {Promise<any>} The response from the token verification endpoint.
*/
async verify(): Promise<BasicAuthVerificationResponse> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, i am not sure why verify is calling authenticate again?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it calls authenticate() for now to get organizationId (required parameter in verifyUrl); I was thinking maybe storing organizationId to Postgres after calling authenticate() for the first time; I wasn't sure if we've finished database integration... lmk what you think

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we have db but it is not flexible. I am going to change recently

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@faze059
Copy link
Owner

faze059 commented Jan 11, 2024

@ybw0014 What do you think?

Copy link
Collaborator

@ybw0014 ybw0014 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolve merge conflicts

Copy link
Collaborator

@ybw0014 ybw0014 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The username and password should be provided by the user. They should be passed to authenticate(), and we really need to rewrite this method to include DTO, that carries required info.
For oauth providers, the dto only need to have the userId.
For basic auth providers, the dto also need to have the username and password.
And for the future access key, the dto need to have the access key.

@@ -8,18 +8,24 @@ import { CreateCredentialDto } from "./database-integration/dto/create-credentia
import { ValidateCredentialPipe } from "./common/pipes/validate-credential.pipe";
import { ExternalAuthIntegrationService } from "./external-auth-integration/external-auth-integration.service";

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Comment on lines 1 to 5
import { BasicAuthString } from "~/external-auth-integration/auth-providers/basic-auth/@types";

export class BasicAuthVerifyDto {
providerKey: BasicAuthString;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can just extend BasicAuthDto to reduce duplicated code

Comment on lines 1 to 16
/**
* This interface represents an OAuth 2.0 provider.
*/
export interface IBasicAuth {
/**
* Returns the authentication URL for the OAuth provider.
*/
authenticate(): Promise<any>;

/**
* Verify the access token.
* However, the OAuth 2.0 spec (RFC 6749) doesn't clearly define the interaction between a Resource Server (RS) and Authorization Server (AS) for access token (AT) validation.
* @param accessToken The access token.
*/
verify?(): Promise<any>;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change the docs

@elien2016 elien2016 closed this Jan 24, 2024
@faze059 faze059 reopened this Jan 26, 2024
@faze059 faze059 requested a review from Zhiqi-Yu January 26, 2024 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants