From a9bd52d154b1fd808a9bd71f59b4130fd9034413 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Fri, 7 Jan 2022 23:40:06 +0530 Subject: [PATCH] feat: introduce oauth parsing to openapi --- .../import-export/import/newopenapi.ts | 128 +++++++++++++++++- 1 file changed, 126 insertions(+), 2 deletions(-) diff --git a/packages/hoppscotch-app/helpers/import-export/import/newopenapi.ts b/packages/hoppscotch-app/helpers/import-export/import/newopenapi.ts index b0be040a8..8c2871ddd 100644 --- a/packages/hoppscotch-app/helpers/import-export/import/newopenapi.ts +++ b/packages/hoppscotch-app/helpers/import-export/import/newopenapi.ts @@ -27,6 +27,7 @@ import { Collection, makeCollection } from "~/newstore/collections" const OPENAPI_DEREF_ERROR = "openapi/deref_error" as const +// TODO: URL Import Support // TODO: YAMLLLLLLL import support!!!!! // TODO: Oauth! @@ -252,7 +253,74 @@ const resolveOpenAPIV3SecurityObj = ( } } } else if (scheme.type === "oauth2") { - // TODO: Implement Oauth + // NOTE: We select flow on a first come basis on this order, authorizationCode > implicit > password > clientCredentials + if (scheme.flows.authorizationCode) { + return { + authType: "oauth-2", + authActive: true, + accessTokenURL: scheme.flows.authorizationCode.tokenUrl ?? "", + authURL: scheme.flows.authorizationCode.authorizationUrl ?? "", + clientID: "", + oidcDiscoveryURL: "", + scope: _schemeData.join(" "), + token: "", + } + } else if (scheme.flows.implicit) { + return { + authType: "oauth-2", + authActive: true, + authURL: scheme.flows.implicit.authorizationUrl ?? "", + accessTokenURL: "", + clientID: "", + oidcDiscoveryURL: "", + scope: _schemeData.join(" "), + token: "", + } + } else if (scheme.flows.password) { + return { + authType: "oauth-2", + authActive: true, + authURL: "", + accessTokenURL: scheme.flows.password.tokenUrl ?? "", + clientID: "", + oidcDiscoveryURL: "", + scope: _schemeData.join(" "), + token: "", + } + } else if (scheme.flows.clientCredentials) { + return { + authType: "oauth-2", + authActive: true, + accessTokenURL: scheme.flows.clientCredentials.tokenUrl ?? "", + authURL: "", + clientID: "", + oidcDiscoveryURL: "", + scope: _schemeData.join(" "), + token: "", + } + } else { + return { + authType: "oauth-2", + authActive: true, + accessTokenURL: "", + authURL: "", + clientID: "", + oidcDiscoveryURL: "", + scope: _schemeData.join(" "), + token: "", + } + } + } else if (scheme.type === "openIdConnect") { + return { + authType: "oauth-2", + authActive: true, + accessTokenURL: "", + authURL: "", + clientID: "", + oidcDiscoveryURL: scheme.openIdConnectUrl ?? "", + scope: _schemeData.join(" "), + token: "", + } } return { authType: "none", authActive: true } @@ -323,7 +391,63 @@ const resolveOpenAPIV2SecurityScheme = ( value: "", } } else if (scheme.type === "oauth2") { - // TODO: Implement OAuth2 + // NOTE: We select flow on a first come basis on this order, accessCode > implicit > password > application + if (scheme.flow === "accessCode") { + return { + authType: "oauth-2", + authActive: true, + accessTokenURL: scheme.tokenUrl ?? "", + authURL: scheme.authorizationUrl ?? "", + clientID: "", + oidcDiscoveryURL: "", + scope: _schemeData.join(" "), + token: "", + } + } else if (scheme.flow === "implicit") { + return { + authType: "oauth-2", + authActive: true, + accessTokenURL: "", + authURL: scheme.authorizationUrl ?? "", + clientID: "", + oidcDiscoveryURL: "", + scope: _schemeData.join(" "), + token: "", + } + } else if (scheme.flow === "application") { + return { + authType: "oauth-2", + authActive: true, + accessTokenURL: scheme.tokenUrl ?? "", + authURL: "", + clientID: "", + oidcDiscoveryURL: "", + scope: _schemeData.join(" "), + token: "", + } + } else if (scheme.flow === "password") { + return { + authType: "oauth-2", + authActive: true, + accessTokenURL: scheme.tokenUrl ?? "", + authURL: "", + clientID: "", + oidcDiscoveryURL: "", + scope: _schemeData.join(" "), + token: "", + } + } else { + return { + authType: "oauth-2", + authActive: true, + accessTokenURL: "", + authURL: "", + clientID: "", + oidcDiscoveryURL: "", + scope: _schemeData.join(" "), + token: "", + } + } } return { authType: "none", authActive: true }