microsoft/typespec

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
abd213cd488a512fc2da78fc209f25cbf235ae41

Branches

Tags

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

Clone

HTTPS

Download ZIP

packages/http/lib/auth.tsp

196lines · 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 */
26@doc("")
27model BasicAuth {
28 @doc("Http authentication")
29 type: AuthType.http;
30
31 @doc("basic auth scheme")
32 scheme: "basic";
33}
34
35/**
36 * Bearer authentication (also called token authentication) is an HTTP authentication scheme that involves security tokens called bearer tokens.
37 * 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.
38 * The client must send this token in the Authorization header when making requests to protected resources:
39 * ```
40 * Authorization: Bearer <token>
41 * ```
42 */
43@doc("")
44model BearerAuth {
45 @doc("Http authentication")
46 type: AuthType.http;
47
48 @doc("bearer auth scheme")
49 scheme: "bearer";
50}
51
52@doc("Describes the location of the API key")
53enum ApiKeyLocation {
54 @doc("API key is a header value")
55 header,
56
57 @doc("API key is a query parameter")
58 query,
59
60 @doc("API key is found in a cookie")
61 cookie,
62}
63
64/**
65 * An API key is a token that a client provides when making API calls. The key can be sent in the query string:
66 *
67 * ```
68 * GET /something?api_key=abcdef12345
69 * ```
70 *
71 * or as a request header
72 *
73 * ```
74 * GET /something HTTP/1.1
75 * X-API-Key: abcdef12345
76 * ```
77 *
78 * or as a cookie
79 *
80 * ```
81 * GET /something HTTP/1.1
82 * Cookie: X-API-KEY=abcdef12345
83 * ```
84 *
85 * @template Location The location of the API key
86 * @template Name The name of the API key
87 */
88@doc("")
89model ApiKeyAuth<Location extends ApiKeyLocation, Name extends string> {
90 @doc("API key authentication")
91 type: AuthType.apiKey;
92
93 @doc("location of the API key")
94 in: Location;
95
96 @doc("name of the API key")
97 name: Name;
98}
99
100/**
101 * OAuth 2.0 is an authorization protocol that gives an API client limited access to user data on a web server.
102 *
103 * 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.
104 * 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.
105 * For more information about OAuth 2.0, see oauth.net and RFC 6749.
106 *
107 * @template Flows The list of supported OAuth2 flows
108 */
109@doc("")
110model OAuth2Auth<Flows extends OAuth2Flow[]> {
111 @doc("OAuth2 authentication")
112 type: AuthType.oauth2;
113
114 @doc("Supported OAuth2 flows")
115 flows: Flows;
116}
117
118@doc("Describes the OAuth2 flow type")
119enum OAuth2FlowType {
120 @doc("authorization code flow")
121 authorizationCode,
122
123 @doc("implcit flow")
124 implicit,
125
126 @doc("password flow")
127 password,
128
129 @doc("client credential flow")
130 clientCredentials,
131}
132
133alias OAuth2Flow = AuthorizationCodeFlow | ImplicitFlow | PasswordFlow | ClientCredentialsFlow;
134
135@doc("Authorization Code flow")
136model AuthorizationCodeFlow {
137 @doc("authorization code flow")
138 type: OAuth2FlowType.authorizationCode;
139
140 @doc("the authorization URL")
141 authorizationUrl: string;
142
143 @doc("the token URL")
144 tokenUrl: string;
145
146 @doc("the refresh URL")
147 refreshUrl?: string;
148
149 @doc("list of scopes for the credential")
150 scopes: string[];
151}
152
153@doc("Implicit flow")
154model ImplicitFlow {
155 @doc("implicit flow")
156 type: OAuth2FlowType.implicit;
157
158 @doc("the authorization URL")
159 authorizationUrl: string;
160
161 @doc("the refresh URL")
162 refreshUrl?: string;
163
164 @doc("list of scopes for the credential")
165 scopes: string[];
166}
167
168@doc("Resource Owner Password flow")
169model PasswordFlow {
170 @doc("password flow")
171 type: OAuth2FlowType.password;
172
173 @doc("the authorization URL")
174 authorizationUrl: string;
175
176 @doc("the refresh URL")
177 refreshUrl?: string;
178
179 @doc("list of scopes for the credential")
180 scopes: string[];
181}
182
183@doc("Client credentials flow")
184model ClientCredentialsFlow {
185 @doc("client credential flow")
186 type: OAuth2FlowType.clientCredentials;
187
188 @doc("the token URL")
189 tokenUrl: string;
190
191 @doc("the refresh URL")
192 refreshUrl?: string;
193
194 @doc("list of scopes for the credential")
195 scopes: string[];
196}
197