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

feat(core): add oidc auth code flow [release] #587

Merged
merged 10 commits into from
Jul 15, 2021
Merged

Conversation

vegardok
Copy link
Contributor

@vegardok vegardok commented Jun 22, 2021

BREAKING CHANGE: loginWithOAuth signature is changed

@jarlah jarlah self-assigned this Jun 22, 2021
@vegardok vegardok force-pushed the add-oidc-auth-code-flow branch 3 times, most recently from 65ac887 to d9af8e5 Compare June 23, 2021 13:41
@vegardok vegardok changed the title Add oidc auth code flow feat! add oidc auth code flow Jun 23, 2021
@vegardok vegardok marked this pull request as ready for review June 23, 2021 14:03
@vegardok vegardok requested a review from a team as a code owner June 23, 2021 14:03
@vegardok vegardok requested review from a team and begozcan June 23, 2021 14:03
@vegardok vegardok changed the title feat! add oidc auth code flow feat!: add oidc auth code flow Jun 23, 2021
@vegardok vegardok force-pushed the add-oidc-auth-code-flow branch 2 times, most recently from c0776ae to 15f3a28 Compare June 23, 2021 14:23
@vegardok vegardok requested a review from jarlah June 23, 2021 14:23
@codecov
Copy link

codecov bot commented Jun 23, 2021

Codecov Report

Merging #587 (e5fc767) into master (00c73e4) will decrease coverage by 22.96%.
The diff coverage is 85.58%.

@@             Coverage Diff             @@
##           master     #587       +/-   ##
===========================================
- Coverage   98.69%   75.72%   -22.97%     
===========================================
  Files          19       86       +67     
  Lines         764     3077     +2313     
  Branches       51      470      +419     
===========================================
+ Hits          754     2330     +1576     
- Misses         10      719      +709     
- Partials        0       28       +28     
Impacted Files Coverage Δ
packages/stable/src/api/sequences/sequencesApi.ts 100.00% <ø> (ø)
...able/src/api/serviceAccounts/serviceAccountsApi.ts 100.00% <ø> (ø)
...table/src/api/timeSeries/syntheticTimeSeriesApi.ts 100.00% <ø> (ø)
...ackages/stable/src/api/timeSeries/timeSeriesApi.ts 100.00% <ø> (ø)
packages/stable/src/cogniteClient.ts 100.00% <ø> (ø)
packages/stable/src/retryValidator.ts 100.00% <ø> (ø)
packages/stable/src/types.ts 100.00% <ø> (ø)
packages/template/src/__tests__/testUtils.ts 100.00% <ø> (ø)
packages/template/src/cogniteClient.ts 100.00% <ø> (ø)
packages/wells/src/__tests__/testUtils.ts 50.00% <ø> (ø)
... and 180 more

Copy link
Contributor

@jarlah jarlah left a comment

Choose a reason for hiding this comment

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

looks good. Have some change requests. and/or comments

@@ -589,7 +631,7 @@ export default class BaseCogniteClient {
user = null;
}

if (!user || !user.access_token) {
if (!user || user.access_token) {
Copy link
Contributor

Choose a reason for hiding this comment

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

is this intentional leaving out the !in front of user.access_token? did it came from my branch 🙈

Copy link
Contributor

Choose a reason for hiding this comment

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

so if we don't have a user or a user WITH access token .... I think this is wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea, looks like a typo to me, good catch

@@ -57,7 +57,7 @@ function App() {

useEffect(() => {
const login = async (client) => {
const result = await client.loginWithOAuth({
const result = await client.loginWithOAuth('ADFS_OAUTH', {
Copy link
Contributor

Choose a reason for hiding this comment

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

needs to be updated to send in flow object

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@@ -61,7 +61,7 @@ function App() {

useEffect(() => {
const login = async (client) => {
const result = await client.loginWithOAuth({
const result = await client.loginWithOAuth('AAD_OAUTH', {
Copy link
Contributor

Choose a reason for hiding this comment

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

needs to be updated to send in flow object

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

* }
* });
*
* // or with client credentials
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not a part of this PR but will be a part of my PR. possibly remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


export type AuthFlowType =
Copy link
Contributor

Choose a reason for hiding this comment

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

so glad we did this. 🙌 much more readable and only by using a basic union with common fields

Copy link

Choose a reason for hiding this comment

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

Agree — this approach looks great

const client = useMemo(() => new CogniteClient({ appId: 'sample-app-id' }), []);

useEffect(() => {
client.loginWithOAuth('OIDC_AUTHORIZATION_CODE_FLOW', {
Copy link
Contributor

Choose a reason for hiding this comment

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

needs to be updated to send in flow object

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@jarlah jarlah removed their assignment Jun 24, 2021
@jarlah
Copy link
Contributor

jarlah commented Jun 25, 2021

I have been doing a lot of thinking on the flow manager approach in the code. So, we have a flow manager and a method inside the base client that handles the flow. So the logic is spread into two places, the flow manager and the loginWith[FlowType] method. I wonder, is it possible to merge the contents of the loginWith[FlowType] method into the flow manager? We could still continue to "manually" resolve the flow manager in the switch, but we wouldn't need to define a method in the base client. One way to avoid creating this method is to pass functions for setting bearer token and cluster.

This could be solved in the following manner:

      case 'OIDC_VENDOR_GENERIC_CLIENT_CREDENTIALS_FLOW': {
        this.vendorGenericFlowManager = new OidcVendorGeneric(flow.options, this.setCluster, this.setBearerToken);
        [authenticate, token] = await this.vendorGenericFlowManager.init();
        break;
      }

where this.setCluster and this.setBearerToken is lambda functions on the class (possible to pass around without lossing this context). Or even just pass them as (cluster: string) => this.httpClient.setCluster(cluster)or this.httpClient.setCluster(the latter if the setCluster in httpClient is a lambda.

the init method name might need to renamed to something else because some flow managers already have an init method. But the point remains.

@vegardok vegardok force-pushed the add-oidc-auth-code-flow branch 2 times, most recently from 067dcbe to 711f6c1 Compare July 13, 2021 10:13
Copy link
Collaborator

@polomani polomani left a comment

Choose a reason for hiding this comment

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

As I mentioned before, my biggest concern is about a breaking change in general.
We have working adfs/aad flow, and breaking the users just for a cleaner code's sake is not really a good excuse. I doubt we have resources to backport bugfixes/features anyway, so by releasing this we just make external developer's life harder.

I would keep the same method we had before, and just introduce an additional one.
Something like setLoginConfig(...) which wouldn't support API keys/legacy OAuth.
As I remember that was what we agreed on earlier.

@vegardok
Copy link
Contributor Author

We have working adfs/aad flow, and breaking the users just for a cleaner code's sake is not really a good excuse.

The main purpose of the PR is to add OIDC support for Aize/Auth0. The API change is not motivated by clean code, but less brittle code. this is error prone, and the more flows you add, the more error prone it will be.

@polomani
Copy link
Collaborator

polomani commented Jul 13, 2021

and the more flows you add, the more error prone it will be

I completely agree, therefore I suggested adding a separate function to handle all these different flows (all of them except the legacy one)

CDF_OAUTH will be deprecated in a couple of months, another major?

@vegardok
Copy link
Contributor Author

and the more flows you add, the more error prone it will be

I completely agree, therefore I suggested adding a separate function to handle all these different flows (all of them except the legacy one)

That would still be a breaking change for AAD/ADFS, so I don't see that as an alternative to the objection against frequent major version updates. I think the current setup is a good fit for changing flows in the future, e.g adding client credentials will be a minor change.

The way I see it, this PR will be breaking and is an improvement. I don't see why adding a new method instead of changing the API is better?

CDF_OAUTH will be deprecated in a couple of months, another major?

Q1 22 at the earliest, but that is optimistic. Or, it has been deprecated since Q1 21, but I don't think it will be shut down before the last customer has migrated. So who knows.

Something like setLoginConfig(...) which wouldn't support API keys/legacy OAuth.

This is also not correct, you are doing more than just setting the config (checking old tokens, trying to silently refresh them if possible). So the semantics of the existing method is a better fit.

@vegardok vegardok changed the title feat!: add oidc auth code flow feat!: add oidc auth code flow [release] Jul 14, 2021
@vegardok vegardok changed the title feat!: add oidc auth code flow [release] feat(core): add oidc auth code flow [release] Jul 14, 2021
@vegardok vegardok requested a review from a team July 14, 2021 12:55
@vegardok vegardok requested a review from a team as a code owner July 14, 2021 12:55
@vegardok vegardok merged commit 0cc44aa into master Jul 15, 2021
@vegardok vegardok deleted the add-oidc-auth-code-flow branch October 12, 2021 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants