microsoft/typespec

Public

mirrored from https://github.com/microsoft/typespecAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3e669c74c1e16a47afe2b98706bdcf8dad4434af

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

packages/http/lib/auth.tsp

185lines · modecode

1namespace TypeSpec.Http;
2
3@doc("Authentication type")
4enum AuthType {
5 @doc("HTTP")
6 http,
7
8 @doc("API key")
9 apiKey,
10
11 @doc("OAuth2")
12 oauth2,
13
14 @doc("OpenID connect")
15 openIdConnect,
16}
17
18/**
19 * Basic authentication is a simple authentication scheme built into the HTTP protocol.
20 * The client sends HTTP requests with the Authorization header that contains the word Basic word followed by a space and a base64-encoded string username:password.
21 * For example, to authorize as demo / `p@55w0rd` the client would send
22 * ```
23 * Authorization: Basic ZGVtbzpwQDU1dzByZA==
24 * ```
25 */
26model BasicAuth {
27 @doc("Http authentication")
28 type: AuthType.http;
29
30 @doc("basic auth scheme")
31 scheme: "basic";
32}
33
34/**
35 * Bearer authentication (also called token authentication) is an HTTP authentication scheme that involves security tokens called bearer tokens.
36 * The name “Bearer authentication” can be understood as “give access to the bearer of this token.” The bearer token is a cryptic string, usually generated by the server in response to a login request.
37 * The client must send this token in the Authorization header when making requests to protected resources:
38 * ```
39 * Authorization: Bearer <token>
40 * ```
41 */
42model BearerAuth {
43 @doc("Http authentication")
44 type: AuthType.http;
45
46 @doc("bearer auth scheme")
47 scheme: "bearer";
48}
49
50@doc("Describes the location of the API key")
51enum ApiKeyLocation {
52 @doc("API key is a header value")
53 header,
54
55 @doc("API key is a query parameter")
56 query,
57
58 @doc("API key is found in a cookie")
59 cookie,
60}
61
62/**
63 * An API key is a token that a client provides when making API calls. The key can be sent in the query string:
64 * ```
65 * GET /something?api_key=abcdef12345
66 * ```
67 *
68 * or as a request header
69 *
70 * ```
71 * GET /something HTTP/1.1
72 * X-API-Key: abcdef12345
73 * ```
74 *
75 * or as a cookie
76 *
77 * ```
78 * GET /something HTTP/1.1
79 * Cookie: X-API-KEY=abcdef12345
80 * ```
81 */
82model ApiKeyAuth<TLocation extends ApiKeyLocation, TName extends string> {
83 @doc("API key authentication")
84 type: AuthType.apiKey;
85
86 @doc("location of the API key")
87 in: TLocation;
88
89 @doc("name of the API key")
90 name: TName;
91}
92
93/**
94 * OAuth 2.0 is an authorization protocol that gives an API client limited access to user data on a web server.
95 * 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.
96 * 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.
97 * For more information about OAuth 2.0, see oauth.net and RFC 6749.
98 */
99model OAuth2Auth<TFlows extends OAuth2Flow[]> {
100 @doc("OAuth2 authentication")
101 type: AuthType.oauth2;
102
103 @doc("Supported OAuth2 flows")
104 flows: TFlows;
105}
106
107@doc("Describes the OAuth2 flow type")
108enum OAuth2FlowType {
109 @doc("authorization code flow")
110 authorizationCode,
111
112 @doc("implcit flow")
113 implicit,
114
115 @doc("password flow")
116 password,
117
118 @doc("client credential flow")
119 clientCredentials,
120}
121
122alias OAuth2Flow = AuthorizationCodeFlow | ImplicitFlow | PasswordFlow | ClientCredentialsFlow;
123
124@doc("Authorization Code flow")
125model AuthorizationCodeFlow {
126 @doc("authorization code flow")
127 type: OAuth2FlowType.authorizationCode;
128
129 @doc("the authorization URL")
130 authorizationUrl: string;
131
132 @doc("the token URL")
133 tokenUrl: string;
134
135 @doc("the refresh URL")
136 refreshUrl?: string;
137
138 @doc("list of scopes for the credential")
139 scopes: string[];
140}
141
142@doc("Implicit flow")
143model ImplicitFlow {
144 @doc("implicit flow")
145 type: OAuth2FlowType.implicit;
146
147 @doc("the authorization URL")
148 authorizationUrl: string;
149
150 @doc("the refresh URL")
151 refreshUrl?: string;
152
153 @doc("list of scopes for the credential")
154 scopes: string[];
155}
156
157@doc("Resource Owner Password flow")
158model PasswordFlow {
159 @doc("password flow")
160 type: OAuth2FlowType.password;
161
162 @doc("the authorization URL")
163 authorizationUrl: string;
164
165 @doc("the refresh URL")
166 refreshUrl?: string;
167
168 @doc("list of scopes for the credential")
169 scopes: string[];
170}
171
172@doc("Client credentials flow")
173model ClientCredentialsFlow {
174 @doc("client credential flow")
175 type: OAuth2FlowType.clientCredentials;
176
177 @doc("the token URL")
178 tokenUrl: string;
179
180 @doc("the refresh URL")
181 refreshUrl?: string;
182
183 @doc("list of scopes for the credential")
184 scopes: string[];
185}
186