diff --git a/cli/example/cookie-auth.yaml b/cli/example/cookie-auth.yaml new file mode 100644 index 00000000..d767fba4 --- /dev/null +++ b/cli/example/cookie-auth.yaml @@ -0,0 +1,28 @@ +openapi: 3.0.4 + +info: + title: Cookie auth + version: 1.0.0 +paths: + /users: + get: + security: + - cookieAuth: [] + description: Returns a list of users. + responses: + "200": + description: OK + content: + application/json: + schema: + type: string + +components: + securitySchemes: + cookieAuth: + type: apiKey + in: cookie + name: COOKIE-MONSTER! + +security: + - cookieAuth: [] diff --git a/cli/src/TestGenScript.elm b/cli/src/TestGenScript.elm index 351e8917..e5daabb0 100644 --- a/cli/src/TestGenScript.elm +++ b/cli/src/TestGenScript.elm @@ -86,6 +86,10 @@ run = nullableEnum = OpenApi.Config.inputFrom (OpenApi.Config.File "./example/nullable-enum.yaml") + cookieAuth : OpenApi.Config.Input + cookieAuth = + OpenApi.Config.inputFrom (OpenApi.Config.File "./example/cookie-auth.yaml") + config : OpenApi.Config.Config config = OpenApi.Config.init "./generated" @@ -106,6 +110,7 @@ run = |> OpenApi.Config.withInput anyOfEnums |> OpenApi.Config.withInput binaryResponse |> OpenApi.Config.withInput nullableEnum + |> OpenApi.Config.withInput cookieAuth in Pages.Script.withoutCliOptions (BackendTask.doEach diff --git a/src/OpenApi/Generate.elm b/src/OpenApi/Generate.elm index 0cb115df..46ed659c 100644 --- a/src/OpenApi/Generate.elm +++ b/src/OpenApi/Generate.elm @@ -1468,7 +1468,31 @@ operationToAuthorizationInfo operation = |> CliMonad.succeed OpenApi.SecurityScheme.Cookie -> - CliMonad.todoWithDefault acc "Unsupported security schema: ApiKey in Cookie" + if Dict.member "Cookie" acc.headers then + CliMonad.todoWithDefault acc "Cookie header already set" + + else + CliMonad.succeed + { acc + | headers = + Dict.insert "Cookie" + (\config -> + Elm.Op.append + (Elm.string (apiKey.name ++ "=")) + (config + |> Elm.get "authorization" + |> Elm.get cleanName + ) + ) + acc.headers + , params = + Dict.insert "authorization" + (Dict.insert cleanName Elm.Annotation.string <| + Maybe.withDefault Dict.empty <| + Dict.get "authorization" acc.params + ) + acc.params + } OpenApi.SecurityScheme.Http details -> case details.scheme of