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

Operation level authentication and scopes #2901

Merged
merged 3 commits into from
Feb 27, 2024

Conversation

susliko
Copy link
Contributor

@susliko susliko commented Feb 8, 2024

Hi! 🖖🏻
This PR resolves #2624 by implementing the design doc, but in its initial form:

  • @useAuth can now be applied not only to service namespace, but to interfaces and operations as well. Its arguments override all authentication, which was set for enclosing scopes.
  • OAuth2 scopes can now be set at operation level (though, the code doing this in OpenAPI emitter is a bit clunky).
  • New NoAuth authentication option allows to declare optional authentication (NoAuth | AnyOtherAuth) or override authentication to none in nested scopes.

This implementation does not introduce new @authScopes decorator as design doc comments suggest, and here's why:

  1. It does not compose well with @useAuth at operation level. For example
...
@useAuth(BasicAuth)
@authScopes(MyOauth2, ["read"])
op gogo(): void

Should that be equivalent to BasicAuth | MyOauth2, or to [BasicAuth, MyOauth2]?

  1. Introducing new decorator would increase complexity, but (imho) it would not reduce the amount of boilerplate:
alias MyOAuth2 = OAuth2Auth<{ ... }>;

@useAuth(MyOAuth2)
@authAcopes(MyOauth2, ["read"])
@service
namepsace Foo;

vs

model MyOAuth2Flow<T extends string[]>  {  ...  };
alias MyOauth2<T extends string[]> = Oauth2Auth<[MyOauth2Flow[T]]>

@useAuth(MyOAuth2<["read"]>)
@service
namepsace Foo

I would be happy to hear any feedback and apply suggested changes.

And thanks for a convenient development setup and thorough test coverage!

@susliko
Copy link
Contributor Author

susliko commented Feb 8, 2024

@microsoft-github-policy-service agree

@microsoft microsoft deleted a comment from azure-pipelines bot Feb 9, 2024
@azure-sdk
Copy link
Collaborator

❌ There is undocummented changes. Run chronus add to add a changeset or click here.

The following packages have changes but are not documented.

  • @typespec/http
  • @typespec/openapi3

@azure-sdk
Copy link
Collaborator

You can try these changes at https://cadlplayground.z22.web.core.windows.net/prs/2901/

Check the website changes at https://tspwebsitepr.z22.web.core.windows.net/prs/2901/

@susliko susliko force-pushed the flexible-auth branch 5 times, most recently from 8417dc8 to 9297247 Compare February 12, 2024 18:27
Copy link
Member

@timotheeguerin timotheeguerin left a comment

Choose a reason for hiding this comment

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

Hi @susliko this starts to look pretty good, I think I have a bit of feedback on the new resolve api and what I think might look better. Let me know what you think.

I am trying to review your deviation of the approved design with our team but I am optimistic we should be able to get that approved and merged for the next(march) release.

packages/http/src/types.ts Show resolved Hide resolved
packages/http/src/types.ts Outdated Show resolved Hide resolved
packages/http/src/types.ts Outdated Show resolved Hide resolved
packages/http/src/auth.ts Outdated Show resolved Hide resolved
@susliko susliko force-pushed the flexible-auth branch 2 times, most recently from 4768936 to 49dbb07 Compare February 22, 2024 11:42
@susliko susliko force-pushed the flexible-auth branch 2 times, most recently from 0561528 to 2aea817 Compare February 26, 2024 12:59
susliko and others added 3 commits February 27, 2024 12:46
Resolves microsoft#2624

Major changes:
- `@useAuth` can now be used both at interface and operation levels
- OAuth2 scopes can be overriden at operation level
@timotheeguerin timotheeguerin merged commit d2d397c into microsoft:main Feb 27, 2024
11 checks passed
markcowl pushed a commit to markcowl/cadl that referenced this pull request Mar 8, 2024
Hi! 🖖🏻 
This PR resolves microsoft#2624 by implementing the [design
doc](https://gist.github.com/timotheeguerin/56690786e61a436710dd647de9febc0f),
but in its initial form:
- `@useAuth` can now be applied not only to service namespace, but to
interfaces and operations as well. Its arguments override all
authentication, which was set for enclosing scopes.
- OAuth2 scopes can now be set at operation level (though, the code
doing this in OpenAPI emitter is a bit clunky).
- New `NoAuth` authentication option allows to declare optional
authentication (`NoAuth | AnyOtherAuth`) or override authentication to
none in nested scopes.

This implementation does not introduce new `@authScopes` decorator as
design doc comments suggest, and here's why:

1. It does not compose well with `@useAuth` at operation level. For
example
```
...
@useAuth(BasicAuth)
@authScopes(MyOauth2, ["read"])
op gogo(): void
```
Should that be equivalent to `BasicAuth | MyOauth2`, or to `[BasicAuth,
MyOauth2]`?

2. Introducing new decorator would increase complexity, but (imho) it
would not reduce the amount of boilerplate:
```
alias MyOAuth2 = OAuth2Auth<{ ... }>;

@useAuth(MyOAuth2)
@authAcopes(MyOauth2, ["read"])
@service
namepsace Foo;
```
vs
```
model MyOAuth2Flow<T extends string[]>  {  ...  };
alias MyOauth2<T extends string[]> = Oauth2Auth<[MyOauth2Flow[T]]>

@useAuth(MyOAuth2<["read"]>)
@service
namepsace Foo
```

I would be happy to hear any feedback and apply suggested changes.

And thanks for a convenient development setup and thorough test
coverage!

---------

Co-authored-by: Timothee Guerin <timothee.guerin@outlook.com>
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.

Implementation: Operation level security
3 participants