Skip to content

Adds support for signing in with multiple identities. Closes #3587#5790

Closed
martinlingstuyl wants to merge 7 commits intopnp:mainfrom
martinlingstuyl:multiple-connections
Closed

Adds support for signing in with multiple identities. Closes #3587#5790
martinlingstuyl wants to merge 7 commits intopnp:mainfrom
martinlingstuyl:multiple-connections

Conversation

@martinlingstuyl
Copy link
Copy Markdown
Contributor

@martinlingstuyl martinlingstuyl commented Jan 23, 2024

Closes #3587

🚀 There we go again! Adding multiple connection functionality 🚀

  • Added logic to allow to login to multiple identities
  • Added a command to list available connections
  • Added a command to switch to another connection
  • Added a command to update a connection name
  • Added a command to remove a connection
  • Added an option to assign a connection name when signing in.
  • Added a message about what identity you're using when running in debug mode.
  • Added connectionName to the output of commands like m365 status and m365 login.
  • The connection use output is the same as m365 status

Points of attention

  • We need to test if Managed Identity still works. That's the only one I haven't tested yet.

  • No graceful fallback I've changed the shape of the data that's saved to our token caches. As a result I've decided (with Waldek) to just use different filenames. So after installing everyone needs to run m365 login again.

Specifically I've added:

Name Explanation
name The name of the connection. By default this is the localAccountId of the associated account or the objectId of the Entra principal. (so unique, also in b2b scenario's)
identityName The name of the identity, can be a userName or application name
identityId The localAccountId of the associated account or just the objectId of the AAD principal. Is necessary to locate the identity and find it in the MSAL store.

Notes for reviewers

  • I've overridden the base action on the connection <verb> command. The reason is that we need to be able to execute these commands when we don't have an active connection. So for example: If I have two connections and I remove one (which is also my active connection) I'll be left in a semi-logged-out state. I need to be able to execute connection use to switch to another connection, hence I'm overriding the base command action.

  • There's some changes in test cases that seem to be unrelated to the PR. For example: spo page add, spo page set, spo site ensure, todo task add, todo task set. These changes are due to issues that were invisible till now. (missing stubs that cause issues with authentication for example)

@martinlingstuyl martinlingstuyl marked this pull request as draft January 23, 2024 08:48
@martinlingstuyl martinlingstuyl force-pushed the multiple-connections branch 2 times, most recently from 1ca38ec to 03a254e Compare January 23, 2024 09:03
@martinlingstuyl martinlingstuyl added the pr-priority Process this PR asap label Jan 23, 2024
@martinlingstuyl martinlingstuyl marked this pull request as ready for review January 23, 2024 09:07
@waldekmastykarz waldekmastykarz self-assigned this Feb 4, 2024
@waldekmastykarz
Copy link
Copy Markdown
Member

Hey @martinlingstuyl, is this PR right? Seems like we've got some extra files modified that aren't a part of the proposed change. Could you please have another look?

@waldekmastykarz waldekmastykarz marked this pull request as draft February 10, 2024 09:09
@martinlingstuyl
Copy link
Copy Markdown
Contributor Author

Can you name the ones you mean? I know I had issues with some tests, which caused me to do more changes than what seems necessary

@martinlingstuyl
Copy link
Copy Markdown
Contributor Author

Also: do check out the PR description where I mention a few of those

@waldekmastykarz
Copy link
Copy Markdown
Member

Can you name the ones you mean? I know I had issues with some tests, which caused me to do more changes than what seems necessary

I'm seeing 777 files changed and included in the PR, a bunch of them are docs. Are all of those changes necessary? Also, it seems like we've got some conflicts. 😊

@waldekmastykarz
Copy link
Copy Markdown
Member

Can you name the ones you mean? I know I had issues with some tests, which caused me to do more changes than what seems necessary

I'm seeing 777 files changed and included in the PR, a bunch of them are docs. Are all of those changes necessary? Also, it seems like we've got some conflicts. 😊

My bad, the files seem to be modified tests. All good. If you could please resolve conflicts, then I'll start the review.

@martinlingstuyl martinlingstuyl force-pushed the multiple-connections branch 2 times, most recently from 823e33c to 54a27f3 Compare February 10, 2024 15:01
@martinlingstuyl martinlingstuyl marked this pull request as ready for review February 10, 2024 15:30
@martinlingstuyl
Copy link
Copy Markdown
Contributor Author

done @waldekmastykarz !

Copy link
Copy Markdown
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.

Very nice! Let's do a couple of changes before we merge.

In general, let's pay attention to the following:

  • let's use single quotes where possible. If we need to escape an apostrophe, let's use backticks
  • let's use a single assert per test. Otherwise, if a test fails, the only way to find out what's wrong is to debug it, which isn't convenient
  • let's avoid to use ! to suppress null validation. It can lead to runtime errors, which we could otherwise catch design time thanks to typings

Comment thread docs/docs/cmd/connection/connection-remove.mdx
Comment thread docs/docs/cmd/connection/connection-list.mdx Outdated
Comment thread docs/docs/cmd/connection/connection-list.mdx
Comment thread src/Auth.ts
Comment thread src/Auth.ts
Comment thread src/m365/connection/commands/connection-use.spec.ts Outdated
Comment thread src/m365/connection/commands/connection-use.ts Outdated
Comment thread src/m365/connection/commands/connection-use.ts Outdated
Comment thread src/m365/connection/commands/connection-set.ts Outdated
Comment thread src/m365/connection/commands/connection-remove.ts Outdated
@waldekmastykarz waldekmastykarz marked this pull request as draft February 13, 2024 12:12
@martinlingstuyl martinlingstuyl marked this pull request as ready for review February 13, 2024 22:44
@martinlingstuyl
Copy link
Copy Markdown
Contributor Author

Hi @waldekmastykarz, it's kinda hard to find comment replies on GH is my experience, (when there are a lot of comments) if you reply to a resolved comment, could you unresolved it?

Comment thread src/Auth.ts Outdated
throw new CommandError(`The connection name '${newName}' is already in use`);
}

allConnections.forEach(c => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To avoid iterating over all connections twice, let's update the code, so that we store the outcome of allConnections.filter in an array. We can then use it to compare its length first, and if we found only one object, directly update its name.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've changed it a bit, as there's only one entry of the connection available.

Comment thread src/Auth.ts Outdated
@martinlingstuyl
Copy link
Copy Markdown
Contributor Author

Were those all comments you had @waldekmastykarz ? Or is the review not done yet?

@waldekmastykarz
Copy link
Copy Markdown
Member

Were those all comments you had @waldekmastykarz ? Or is the review not done yet?

This was it. You've done awesome job 👏 Let me have another look and we'll get it in for folks to test.

@martinlingstuyl
Copy link
Copy Markdown
Contributor Author

I'll see if the conditionally nullable and assert does a better job instead of the null checks

@martinlingstuyl
Copy link
Copy Markdown
Contributor Author

Ok @waldekmastykarz, I fixed your comments. I also added some assert lines instead of if-statements. See the latest commit. The most interesting thing happened: login tests started failing, because the code was run, while these properties were not available. I corrected that in the login tests.

@waldekmastykarz
Copy link
Copy Markdown
Member

Just to double check: is the PR ready for review?

@martinlingstuyl
Copy link
Copy Markdown
Contributor Author

Yes it is! Unless I must first fix the merge conflicts 😀

@martinlingstuyl
Copy link
Copy Markdown
Contributor Author

All yours @waldekmastykarz!

Copy link
Copy Markdown
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.

I've just noticed an undesired side-effect: when you run m365 status and you're not signed in, you get a login prompt. Let's revert this behavior to what we have now (status shows the current state without automatically requiring you to login).

@waldekmastykarz waldekmastykarz marked this pull request as draft February 24, 2024 08:50
@martinlingstuyl
Copy link
Copy Markdown
Contributor Author

Yeah, that's definitely not as it should. I'll check it out and update it

@martinlingstuyl
Copy link
Copy Markdown
Contributor Author

I cannot reproduce this behavior @waldekmastykarz,
I've logged out from all accounts. Running m365 status now shows me "Logged out"
Now I've signed in with two accounts and removed the active one using m365 connection remove. Running the status command now shows me "Logged out, signed in connections available". Running m365 logout again, now the status command again shows me "Logged out".

Can you consistently reproduce it in some way?

@waldekmastykarz waldekmastykarz marked this pull request as ready for review February 25, 2024 13:42
@waldekmastykarz
Copy link
Copy Markdown
Member

I cannot reproduce this behavior @waldekmastykarz, I've logged out from all accounts. Running m365 status now shows me "Logged out" Now I've signed in with two accounts and removed the active one using m365 connection remove. Running the status command now shows me "Logged out, signed in connections available". Running m365 logout again, now the status command again shows me "Logged out".

Can you consistently reproduce it in some way?

Odd, I can't repro it indeed. All seems to be working just fine. I wonder if it's related to the fact, that I was signed in with the current code base and then pulled in your PR, built it and ran the status command. Either way, all seems good. I'll proceed with the review. Thanks for double checking and sorry for the trouble.

@waldekmastykarz
Copy link
Copy Markdown
Member

Merged manually. Brilliant work! So cool to see this out there 👏

@martinlingstuyl martinlingstuyl deleted the multiple-connections branch February 25, 2024 15:31
@martinlingstuyl
Copy link
Copy Markdown
Contributor Author

This is really a game changer indeed. Superb!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-merged pr-priority Process this PR asap

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for signing in using multiple accounts

2 participants