|
| 1 | +--- |
| 2 | +sidebar_position: 20 |
| 3 | +sidebar_label: Bearer Authentication |
| 4 | +--- |
| 5 | + |
| 6 | +import Tabs from '@theme/Tabs'; |
| 7 | +import TabItem from '@theme/TabItem'; |
| 8 | + |
| 9 | +# Bearer Token Authentication |
| 10 | + |
| 11 | +The `SecuritySchemeBearer` type supports bearer token authentication, commonly used with JWT tokens. It can either be |
| 12 | +a simple `string` representing the JWT token or an object with additional metadata if another token format is used. |
| 13 | + |
| 14 | +See the [OpenAPI Bearer Authentication 🔗](https://swagger.io/docs/specification/authentication/bearer-authentication/) specification for more information. |
| 15 | + |
| 16 | +```ts |
| 17 | +type SecuritySchemeBearer = |
| 18 | + | string /** JWT Bearer token **/ |
| 19 | + | { |
| 20 | + /** Refresh interval in milliseconds. */ |
| 21 | + refreshInterval: number; |
| 22 | + /** Token to be used for authentication. */ |
| 23 | + token: string; |
| 24 | + }; |
| 25 | +``` |
| 26 | + |
| 27 | +- **String**: A simple JWT Bearer token string. |
| 28 | + - The JWT must include the `exp` (expiration time) and `iat` (issued at) fields. |
| 29 | + - `QraftSecureRequestFn` will automatically refresh the token if it expires. |
| 30 | + - If the `exp` and `iat` fields are not present, an object token format must be used; otherwise, an error will be thrown. |
| 31 | +- **Object**: An object containing: |
| 32 | + - `refreshInterval`: The interval (in milliseconds) at which the token should be refreshed. |
| 33 | + - `token`: The JWT token to be used for authentication. |
| 34 | + |
| 35 | +### Examples |
| 36 | + |
| 37 | +<Tabs> |
| 38 | + <TabItem value="jwt-bearer" label="JWT Bearer" default> |
| 39 | + When providing a simple JWT Bearer token string, the token will be used as is. `QraftSecureRequestFn` will refresh |
| 40 | + the token if it expires based on the `exp` (expiration time) and `iat` (issued at) fields. |
| 41 | + |
| 42 | + In this example, the `clientToken` security scheme handler is used to obtain and refresh the JWT Bearer token: |
| 43 | + |
| 44 | + ```tsx |
| 45 | + import { QraftSecureRequestFn } from '@openapi-qraft/react/Unstable_QraftSecureRequestFn'; |
| 46 | + import { requestFn } from '@openapi-qraft/react'; |
| 47 | + import { createAPIClient } from './api'; // generated by OpenAPI Qraft CLI |
| 48 | + import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
| 49 | + |
| 50 | + import { useMemo, createContext, type ReactNode } from 'react'; |
| 51 | + |
| 52 | + const App = ({ children }: { children: ReactNode }) => { |
| 53 | + const queryClient = useMemo(() => new QueryClient(), []); |
| 54 | + |
| 55 | + return ( |
| 56 | + <QueryClientProvider client={queryClient}> |
| 57 | + <QraftSecureRequestFn |
| 58 | + requestFn={requestFn} |
| 59 | + securitySchemes={{ |
| 60 | + clientToken: async ({ isRefreshing }) => { |
| 61 | + const api = createAPIClient({ |
| 62 | + requestFn, |
| 63 | + baseUrl: 'https://petstore3.swagger.io/api/v3', |
| 64 | + }); |
| 65 | + |
| 66 | + if (!isRefreshing) { |
| 67 | + const { access_token } = await api.auth.obtainToken({ |
| 68 | + body: { |
| 69 | + grant_type: 'client_credentials', // specify `body` or `parameters` if needed |
| 70 | + }, |
| 71 | + }); |
| 72 | + |
| 73 | + // If token is JWT Bearer, return the token string |
| 74 | + // The token will be used in the `Authorization: Bearer <token>` header |
| 75 | + return access_token; |
| 76 | + } |
| 77 | + |
| 78 | + const { access_token } = await api.auth.refreshToken({ |
| 79 | + body: { |
| 80 | + grant_type: 'client_credentials', // specify `body` or `parameters` if needed |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + return access_token; |
| 85 | + } |
| 86 | + }} |
| 87 | + > |
| 88 | + {(secureRequestFn) => { |
| 89 | + // When using `secureRequestFn`, all requests that require the `clientToken` security scheme |
| 90 | + // will automatically include the `Authorization: Bearer <token>` header. |
| 91 | + // This ensures that these requests are authenticated with the provided JWT token. |
| 92 | + const api = createAPIClient({ |
| 93 | + queryClient, |
| 94 | + requestFn: secureRequestFn, |
| 95 | + baseUrl: 'https://petstore3.swagger.io/api/v3', |
| 96 | + }); |
| 97 | + |
| 98 | + return ( |
| 99 | + <MyAPIContext.Provider value={api}> |
| 100 | + {children} |
| 101 | + </MyAPIContext.Provider> |
| 102 | + ) |
| 103 | + }} |
| 104 | + </QraftSecureRequestFn> |
| 105 | + </QueryClientProvider> |
| 106 | + ); |
| 107 | + }; |
| 108 | + |
| 109 | + const MyAPIContext = createContext<ReturnType<typeof createAPIClient>>(null!); |
| 110 | + |
| 111 | + export default App; |
| 112 | + ``` |
| 113 | + </TabItem> |
| 114 | + <TabItem value="custom-token" label="Custom Token"> |
| 115 | + In case the token format is different from the standard JWT Bearer token, you can provide an object with |
| 116 | + `refreshInterval` and `token` properties. |
| 117 | + |
| 118 | + ```tsx |
| 119 | + import { QraftSecureRequestFn } from '@openapi-qraft/react/Unstable_QraftSecureRequestFn'; |
| 120 | + import { requestFn } from '@openapi-qraft/react'; |
| 121 | + import { createAPIClient } from './api'; // generated by OpenAPI Qraft CLI |
| 122 | + import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
| 123 | + |
| 124 | + import { useMemo, createContext, type ReactNode } from 'react'; |
| 125 | + |
| 126 | + const App = ({ children }: { children: ReactNode }) => { |
| 127 | + const queryClient = useMemo(() => new QueryClient(), []); |
| 128 | + |
| 129 | + return ( |
| 130 | + <QueryClientProvider client={queryClient}> |
| 131 | + <QraftSecureRequestFn |
| 132 | + requestFn={requestFn} |
| 133 | + securitySchemes={{ |
| 134 | + clientToken: async ({ isRefreshing }) => { |
| 135 | + const api = createAPIClient({ |
| 136 | + queryClient, |
| 137 | + requestFn, |
| 138 | + baseUrl: 'https://petstore3.swagger.io/api/v3', |
| 139 | + }); |
| 140 | + |
| 141 | + if (!isRefreshing) { |
| 142 | + // Use Qraft's built-in Vanilla operation methods to fetch the token |
| 143 | + const { access_token, expires_in } = await api.auth.obtainToken({ |
| 144 | + body: { |
| 145 | + grant_type: 'client_credentials', // specify `body` or `parameters` if needed |
| 146 | + }, |
| 147 | + }, requestFn); |
| 148 | + |
| 149 | + return { |
| 150 | + refreshInterval: expires_in * 1000, // specify the refresh interval in milliseconds |
| 151 | + token: access_token, // specify the token to be used in the `Authorization: Bearer <token>` header |
| 152 | + }; |
| 153 | + } |
| 154 | + |
| 155 | + const { access_token, expires_in } = await api.auth.refreshToken({ |
| 156 | + body: { |
| 157 | + grant_type: 'client_credentials', |
| 158 | + }, |
| 159 | + }, requestFn); |
| 160 | + |
| 161 | + return { |
| 162 | + refreshInterval: expires_in * 1000, |
| 163 | + token: access_token, |
| 164 | + }; |
| 165 | + } |
| 166 | + }} |
| 167 | + > |
| 168 | + {(secureRequestFn) => { |
| 169 | + // When using `secureRequestFn`, all requests that require the `clientToken` security scheme |
| 170 | + // will automatically include the `Authorization: Bearer <token>` header. |
| 171 | + // This ensures that these requests are authenticated with the provided JWT token. |
| 172 | + const api = createAPIClient({ |
| 173 | + queryClient, |
| 174 | + requestFn: secureRequestFn, |
| 175 | + baseUrl: 'https://petstore3.swagger.io/api/v3', |
| 176 | + }); |
| 177 | + |
| 178 | + return ( |
| 179 | + <MyAPIContext.Provider value={api}> |
| 180 | + {children} |
| 181 | + </MyAPIContext.Provider> |
| 182 | + ) |
| 183 | + }} |
| 184 | + </QraftSecureRequestFn> |
| 185 | + </QueryClientProvider> |
| 186 | + ); |
| 187 | + }; |
| 188 | + |
| 189 | + const MyAPIContext = createContext<ReturnType<typeof createAPIClient>>(null!); |
| 190 | + |
| 191 | + export default App; |
| 192 | + ``` |
| 193 | + </TabItem> |
| 194 | +</Tabs> |
0 commit comments