Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Implements accessToken as new authType. Closes #5816 #6268

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

martinlingstuyl
Copy link
Contributor

Closes #5816

Implements accessToken as new authType.

Ok, this is a work in progress, I haven't implemented tests yet, but I'd really like some opinion here.
I've implemented the basics, you can use one or more access tokens. It doesn't matter which ones you use.

Our logic normally checked if a token for the graph was available during login. For accessToken auth I'm working around that. It checks all tokens that you've passed, it doesn't depend on the graph. If you just use a SharePoint token, you'll be able to execute SharePoint commands, but not graph commands, and the other way around.

Try it out like:

m365 login --authType accessToken --accessToken "ey...1"

or:

m365 login --authType accessToken --accessToken "ey...1" --accessToken "ey...2"

@milanholemans milanholemans marked this pull request as draft August 22, 2024 20:59
@milanholemans
Copy link
Contributor

Cool stuff @martinlingstuyl!
In quite some commands, we're getting the default accesstoken to check if app-only permissions are used. Will this work with your implementation as well?

@martinlingstuyl
Copy link
Contributor Author

Not yet no, I'll need to fix that. Thanks for pointing it out...

Copy link
Member

@waldekmastykarz waldekmastykarz left a comment

Choose a reason for hiding this comment

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

Nice start! Let's fine tune it to make it more efficient

src/Auth.ts Outdated
if (debug) {
if (!accessToken) {
await logger.logToStderr(`No token found for resource ${resource}.`);
// If the authType is accessToken, we handle returning the token later on.
Copy link
Member

Choose a reason for hiding this comment

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

if authType is accessToken, shouldn't we just return the access token and be done with it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's possible, but in that case there's some logic that is skipped, like calling the storeConnectionInfo function and setting the identityId parameters etc. That was the reason I wanted to keep things similar to how they are with the other auth methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've rewritten it slightly. Better like this? We still should not return it right away.

@@ -199,46 +200,43 @@ export class Auth {
}

public async ensureAccessToken(resource: string, logger: Logger, debug: boolean = false, fetchNew: boolean = false): Promise<string> {
const now: Date = new Date();
Copy link
Member

Choose a reason for hiding this comment

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

What if the specified access token expired? I suggest that we still perform this logic as early as possible to catch errors as soon as possible

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't remove the logic, it's still there. also for the new authType

src/Auth.ts Outdated Show resolved Hide resolved
src/Auth.ts Outdated Show resolved Hide resolved
throw `No token found for resource ${resource}.`;
}

if (this.accessTokenExpired(accessToken)) {
Copy link
Member

Choose a reason for hiding this comment

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

We should handle this centrally for all tokens rather than for each auth type separately. The logic is the same and having it done early allows us to speed up auth flow and avoid repetition

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, yes, but in the case of this new authType we want slightly different behavior: I want to throw if it's expired for example. Do you have suggestions on how to do it differently?

src/m365/commands/login.ts Outdated Show resolved Hide resolved
src/m365/commands/login.ts Outdated Show resolved Hide resolved
@@ -151,6 +197,88 @@ class LoginCommand extends Command {
await this.initAction(args, logger);
await this.commandAction(logger, args);
}

private tokensForMultipleTenants(accessTokenValue: string | string[] | undefined, tenantValue: string | undefined): boolean {
Copy link
Member

Choose a reason for hiding this comment

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

Let's rename this command to make it clearer what it does. Right now, without looking at its implementation, it's hard to tell what it's doing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would you have a suggestions here?

Copy link
Member

Choose a reason for hiding this comment

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

How about areTokensForMultipleTenants? Adding a verb makes it clearer what the function does

src/m365/commands/login.ts Outdated Show resolved Hide resolved
src/m365/commands/login.ts Outdated Show resolved Hide resolved
@martinlingstuyl
Copy link
Contributor Author

martinlingstuyl commented Aug 23, 2024

Ok, WIP 2.
I wrote some responses to your comments. I cannot fix everything without some async brainstorming @waldekmastykarz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Signing in through a federated identity
3 participants