You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update http authentication docs with new operation level auth and scopes (microsoft#2960)
PR added support for it but we need to update the docs to explain the
new feature.
---------
Co-authored-by: Brian Terlson <brian.terlson@microsoft.com>
Copy file name to clipboardexpand all lines: docs/libraries/http/authentication.md
+95-23
Original file line number
Diff line number
Diff line change
@@ -4,40 +4,40 @@ title: Authentication
4
4
5
5
# Configure Http Authentication
6
6
7
-
Notes:
7
+
## Configure
8
8
9
-
-Authentication right now can ONLY be configured at the service level.
9
+
Authentication can be configured using the `@useAuth` decorator on a service namespace, sub-namespace, interface or operation.
10
10
11
-
## Configure
11
+
The `@useAuth` decorator takes as input the security to apply for all operations contained in the type. If another `@useAuth` was specified on a parent type the new one will override the value of the parent. [See application hierarchy](#application-hierarchy)
12
12
13
-
Authentication can be configured using the `@useAuth` decorator on the service namespace. The decorator accept a few options:
13
+
The input can be a:
14
14
15
-
- A security scheme (see options [here](https://github.com/microsoft/typespec/blob/main/packages/http/lib/auth.tsp)). This means this is the security scheme to use to authenticate this service.
15
+
- A single security scheme (see options [here](https://github.com/microsoft/typespec/blob/main/packages/http/lib/auth.tsp)).
16
16
17
-
```typespec
18
-
@useAuth(Auth1)
19
-
```
17
+
```typespec
18
+
@useAuth(Auth1)
19
+
```
20
20
21
-
- A tuple of security scheme. This means ALL the different security schemes of the tuple MUST be used together to authenticate this service.
21
+
- A tuple of security schemes. This creates a security group and means **all** the security schemes in the group **must** be used together to authenticate this service.
22
22
23
-
```typespec
24
-
// Use BOTH Auth1 or Auth2
25
-
@useAuth([Auth1, Auth2])
26
-
```
23
+
```typespec
24
+
// Use BOTH Auth1 or Auth2
25
+
@useAuth([Auth1, Auth2])
26
+
```
27
27
28
-
- A union of security scheme. This means EITHER of the security schemes can be used to authenticate this service
28
+
- A union of security schemes. This means **one** of the security schemes **must** be used to authenticate this service.
29
29
30
-
```typespec
31
-
// Use EITHER Auth1 or Auth2
32
-
@useAuth(Auth1 | Auth2)
33
-
```
30
+
```typespec
31
+
// Use EITHER Auth1 or Auth2
32
+
@useAuth(Auth1 | Auth2)
33
+
```
34
34
35
-
- A union of tuple security scheme. This means EITHER of the security groups schemes can be used to authenticate this service
35
+
- A union of security schemes and tuples representing security groups. This means **one** of the security groups or security schemes **must** be used to authenticate this service.
36
36
37
-
```typespec
38
-
// Use EITHER (Auth1 AND Auth2) OR Auth3
39
-
@useAuth([Auth1, Auth2] | Auth3)
40
-
```
37
+
```typespec
38
+
// Use EITHER (Auth1 AND Auth2) OR Auth3
39
+
@useAuth([Auth1, Auth2] | Auth3)
40
+
```
41
41
42
42
## Available security schemes
43
43
@@ -111,3 +111,75 @@ OAuth 2.0 is an authorization protocol that gives an API client limited access t
111
111
OAuth relies on authentication scenarios called flows, which allow the resource owner (user) to share the protected content from the resource server without sharing their credentials.
112
112
For that purpose, an OAuth 2.0 server issues access tokens that the client applications can use to access protected resources on behalf of the resource owner.
113
113
For more information about OAuth 2.0, see oauth.net and RFC 6749.
114
+
115
+
## Application hierarchy
116
+
117
+
The `@useAuth` decorator can be used on a service namespace, sub-namespace, interface or operation. The security scheme specified will be applied to all operations contained in the type.
118
+
A child type with a `@useAuth` will **override** the security scheme of the parent type. If the goal is to append a new scheme, you must re-specify the security schemes on the parent and add the new scheme.
119
+
120
+
### Examples
121
+
122
+
All operations of `MyService` will use `BasicAuth` to authenticate
123
+
124
+
```typespec
125
+
@useAuth(BasicAuth)
126
+
namespace MyService;
127
+
128
+
op one(): void;
129
+
op two(): void;
130
+
```
131
+
132
+
`one` will use `ApiKey` auth and `two` will use the `Basic` auth.
The `OAuth2` security scheme can have a list of scopes that must be granted to the OAuth2 token to be able to use the operations the security scheme applies to.
157
+
When different operations have different scopes, you will likely want to create a template that allows providing OAuth scopes without having to respecify the other properties of the security scheme:
158
+
159
+
```tsp
160
+
alias MyOAuth2<Scopes extends valueof string[]> = OAuth2Auth<[{
Copy file name to clipboardexpand all lines: packages/http/lib/auth.tsp
+1-1
Original file line number
Diff line number
Diff line change
@@ -111,7 +111,7 @@ model ApiKeyAuth<Location extends ApiKeyLocation, Name extends string> {
111
111
* @templateScopes The list of OAuth2 scopes, which are common for every flow from `Flows`. This list is combined with the scopes defined in specific OAuth2 flows.
0 commit comments