git.iko.soy/typeable/openapi3
on master help sign in
Migrate OAuth2 flows
commitfec285b3a40937e6e3c9e94508dd4a43f8daaad3 authorMaxim Koltsov <kolmax94@gmail.com> dateFri 26 Jun 2020, 13:59 (6 years ago) parent5b5cfcb
4 files changed+109−88
src/Data/Swagger.hs+5−2 src/Data/Swagger/Internal.hs+92−78 src/Data/Swagger/Lens.hs+6−7 src/Data/Swagger/Optics.hs+6−1
src/Data/Swagger.hs+5−2
@@ -95,15 +95,18 @@ module Data.Swagger (
9595 SecurityScheme(..),
9696 SecuritySchemeType(..),
9797 SecurityRequirement(..),
98− SecurityDefinitions(..),
9998
10099 -- *** API key
101100 ApiKeyParams(..),
102101 ApiKeyLocation(..),
103102
104103 -- *** OAuth2
105− OAuth2Params(..),
104+ OAuth2Flows(..),
106105 OAuth2Flow(..),
106+ OAuth2ImplicitFlow(..),
107+ OAuth2PasswordFlow(..),
108+ OAuth2ClientCredentialsFlow(..),
109+ OAuth2AuthorizationCodeFlow(..),
107110 AuthorizationURL,
108111 TokenURL,
109112
src/Data/Swagger/Internal.hs+92−78
@@ -838,26 +838,54 @@ type AuthorizationURL = Text
838838-- | The token URL to be used for OAuth2 flow. This SHOULD be in the form of a URL.
839839type TokenURL = Text
840840
841−data OAuth2Flow
842− = OAuth2Implicit AuthorizationURL
843− | OAuth2Password TokenURL
844− | OAuth2Application TokenURL
845− | OAuth2AccessCode AuthorizationURL TokenURL
846− deriving (Eq, Show, Generic, Data, Typeable)
841+data OAuth2ImplicitFlow = OAuth2ImplicitFlow
842+ { _oAuth2ImplicitFlowAuthorizationUrl :: AuthorizationURL
843+ } deriving (Eq, Show, Generic, Data, Typeable)
844+
845+data OAuth2PasswordFlow = OAuth2PasswordFlow
846+ { _oAuth2PasswordFlowTokenUrl :: TokenURL
847+ } deriving (Eq, Show, Generic, Data, Typeable)
848+
849+data OAuth2ClientCredentialsFlow = OAuth2ClientCredentialsFlow
850+ { _oAuth2ClientCredentialsFlowTokenUrl :: TokenURL
851+ } deriving (Eq, Show, Generic, Data, Typeable)
852+
853+data OAuth2AuthorizationCodeFlow = OAuth2AuthorizationCodeFlow
854+ { _oAuth2AuthorizationCodeFlowAuthorizationUrl :: AuthorizationURL
855+ , _oAuth2AuthorizationCodeFlowTokenUrl :: TokenURL
856+ } deriving (Eq, Show, Generic, Data, Typeable)
847857
848−data OAuth2Params = OAuth2Params
849− { -- | The flow used by the OAuth2 security scheme.
850− _oauth2Flow :: OAuth2Flow
858+data OAuth2Flow p = OAuth2Flow
859+ { _oAuth2Params :: p
860+
861+ -- | The URL to be used for obtaining refresh tokens.
862+ , _oAath2RefreshUrl :: Maybe URL
851863
852864 -- | The available scopes for the OAuth2 security scheme.
853− , _oauth2Scopes :: InsOrdHashMap Text Text
865+ -- A map between the scope name and a short description for it.
866+ -- The map MAY be empty.
867+ , _oAuth2Scopes :: InsOrdHashMap Text Text
868+ } deriving (Eq, Show, Generic, Data, Typeable)
869+
870+data OAuth2Flows = OAuth2Flows
871+ { -- | Configuration for the OAuth Implicit flow
872+ _oAuth2FlowsImplicit :: Maybe (OAuth2Flow OAuth2ImplicitFlow)
873+
874+ -- | Configuration for the OAuth Resource Owner Password flow
875+ , _oAuth2FlowsPassword :: Maybe (OAuth2Flow OAuth2PasswordFlow)
876+
877+ -- | Configuration for the OAuth Client Credentials flow
878+ , _oAuth2FlowsClientCredentials :: Maybe (OAuth2Flow OAuth2ClientCredentialsFlow)
879+
880+ -- | Configuration for the OAuth Authorization Code flow
881+ , _oAuth2FlowsAuthorizationCode :: Maybe (OAuth2Flow OAuth2AuthorizationCodeFlow)
854882 } deriving (Eq, Show, Generic, Data, Typeable)
855883
856884data SecuritySchemeType
857885 = SecuritySchemeHttp
858886 | SecuritySchemeApiKey ApiKeyParams
859− | SecuritySchemeOAuth2 OAuth2Params
860−-- | SecuritySchemeOpenIdConnect -- FIXME
887+ | SecuritySchemeOAuth2 OAuth2Flows
888+ | SecuritySchemeOpenIdConnect URL
861889 deriving (Eq, Show, Generic, Data, Typeable)
862890
863891data SecurityScheme = SecurityScheme
@@ -868,22 +896,6 @@ data SecurityScheme = SecurityScheme
868896 , _securitySchemeDescription :: Maybe Text
869897 } deriving (Eq, Show, Generic, Data, Typeable)
870898
871−
872−-- | merge scopes of two OAuth2 security schemes when their flows are identical.
873−-- In other case returns first security scheme
874−mergeSecurityScheme :: SecurityScheme -> SecurityScheme -> SecurityScheme
875−mergeSecurityScheme s1@(SecurityScheme (SecuritySchemeOAuth2 (OAuth2Params flow1 scopes1)) desc)
876− s2@(SecurityScheme (SecuritySchemeOAuth2 (OAuth2Params flow2 scopes2)) _)
877− = if flow1 == flow2 then
878− SecurityScheme (SecuritySchemeOAuth2 (OAuth2Params flow1 (scopes1 <> scopes2))) desc
879− else
880− s1
881−mergeSecurityScheme s1 _ = s1
882−
883−newtype SecurityDefinitions
884− = SecurityDefinitions (Definitions SecurityScheme)
885− deriving (Eq, Show, Generic, Data, Typeable)
886−
887899-- | Lists the required security schemes to execute this operation.
888900-- The object can have multiple security schemes declared in it which are all required
889901-- (that is, there is a logical AND between the schemes).
@@ -951,7 +963,8 @@ data AdditionalProperties
951963
952964deriveGeneric ''Components
953965deriveGeneric ''Header
954−deriveGeneric ''OAuth2Params
966+deriveGeneric ''OAuth2Flow
967+deriveGeneric ''OAuth2Flows
955968deriveGeneric ''Operation
956969deriveGeneric ''Param
957970deriveGeneric ''ParamOtherSchema
@@ -1067,15 +1080,10 @@ instance Monoid Operation where
10671080 mempty = genericMempty
10681081 mappend = (<>)
10691082
1070−instance Semigroup SecurityScheme where
1071− (<>) = mergeSecurityScheme
1072−
1073−instance Semigroup SecurityDefinitions where
1074− (SecurityDefinitions sd1) <> (SecurityDefinitions sd2) =
1075− SecurityDefinitions $ InsOrdHashMap.unionWith (<>) sd1 sd2
1076−
1077−instance Monoid SecurityDefinitions where
1078− mempty = SecurityDefinitions InsOrdHashMap.empty
1083+instance Semigroup OAuth2Flows where
1084+ (<>) = genericMappend
1085+instance Monoid OAuth2Flows where
1086+ mempty = genericMempty
10791087 mappend = (<>)
10801088
10811089-- =======================================================================
@@ -1093,7 +1101,6 @@ instance SwaggerMonoid Responses
10931101instance SwaggerMonoid Response
10941102instance SwaggerMonoid ExternalDocs
10951103instance SwaggerMonoid Operation
1096−instance SwaggerMonoid SecurityDefinitions
10971104instance (Eq a, Hashable a) => SwaggerMonoid (InsOrdHashSet a)
10981105
10991106instance SwaggerMonoid MimeList
@@ -1165,6 +1172,18 @@ instance ToJSON Xml where
11651172instance ToJSON Discriminator where
11661173 toJSON = genericToJSON (jsonPrefix "Discriminator")
11671174
1175+instance ToJSON OAuth2ImplicitFlow where
1176+ toJSON = genericToJSON (jsonPrefix "OAuth2ImplicitFlow")
1177+
1178+instance ToJSON OAuth2PasswordFlow where
1179+ toJSON = genericToJSON (jsonPrefix "OAuth2PasswordFlow")
1180+
1181+instance ToJSON OAuth2ClientCredentialsFlow where
1182+ toJSON = genericToJSON (jsonPrefix "OAuth2ClientCredentialsFlow")
1183+
1184+instance ToJSON OAuth2AuthorizationCodeFlow where
1185+ toJSON = genericToJSON (jsonPrefix "OAuth2AuthorizationCodeFlow")
1186+
11681187-- =======================================================================
11691188-- Simple Generic-based FromJSON instances
11701189-- =======================================================================
@@ -1205,26 +1224,27 @@ instance FromJSON ExternalDocs where
12051224instance FromJSON Discriminator where
12061225 parseJSON = genericParseJSON (jsonPrefix "Discriminator")
12071226
1227+instance FromJSON OAuth2ImplicitFlow where
1228+ parseJSON = genericParseJSON (jsonPrefix "OAuth2ImplicitFlow")
1229+
1230+instance FromJSON OAuth2PasswordFlow where
1231+ parseJSON = genericParseJSON (jsonPrefix "OAuth2PasswordFlow")
1232+
1233+instance FromJSON OAuth2ClientCredentialsFlow where
1234+ parseJSON = genericParseJSON (jsonPrefix "OAuth2ClientCredentialsFlow")
1235+
1236+instance FromJSON OAuth2AuthorizationCodeFlow where
1237+ parseJSON = genericParseJSON (jsonPrefix "OAuth2AuthorizationCodeFlow")
1238+
12081239-- =======================================================================
12091240-- Manual ToJSON instances
12101241-- =======================================================================
12111242
1212−instance ToJSON OAuth2Flow where
1213− toJSON (OAuth2Implicit authUrl) = object
1214− [ "flow" .= ("implicit" :: Text)
1215− , "authorizationUrl" .= authUrl ]
1216− toJSON (OAuth2Password tokenUrl) = object
1217− [ "flow" .= ("password" :: Text)
1218− , "tokenUrl" .= tokenUrl ]
1219− toJSON (OAuth2Application tokenUrl) = object
1220− [ "flow" .= ("application" :: Text)
1221− , "tokenUrl" .= tokenUrl ]
1222− toJSON (OAuth2AccessCode authUrl tokenUrl) = object
1223− [ "flow" .= ("accessCode" :: Text)
1224− , "authorizationUrl" .= authUrl
1225− , "tokenUrl" .= tokenUrl ]
1226−
1227−instance ToJSON OAuth2Params where
1243+instance (Eq p, ToJSON p, AesonDefaultValue p) =>ToJSON (OAuth2Flow p) where
1244+ toJSON = sopSwaggerGenericToJSON
1245+ toEncoding = sopSwaggerGenericToEncoding
1246+
1247+instance ToJSON OAuth2Flows where
12281248 toJSON = sopSwaggerGenericToJSON
12291249 toEncoding = sopSwaggerGenericToEncoding
12301250
@@ -1237,6 +1257,10 @@ instance ToJSON SecuritySchemeType where
12371257 toJSON (SecuritySchemeOAuth2 params)
12381258 = toJSON params
12391259 <+> object [ "type" .= ("oauth2" :: Text) ]
1260+ toJSON (SecuritySchemeOpenIdConnect url) = object
1261+ [ "type" .= ("openIdConnect" :: Text)
1262+ , "openIdConnectUrl" .= url
1263+ ]
12401264
12411265instance ToJSON Swagger where
12421266 toJSON a = sopSwaggerGenericToJSON a &
@@ -1332,9 +1356,6 @@ instance ToJSON Encoding where
13321356 toJSON = sopSwaggerGenericToJSON
13331357 toEncoding = sopSwaggerGenericToEncoding
13341358
1335−instance ToJSON SecurityDefinitions where
1336− toJSON (SecurityDefinitions sd) = toJSON sd
1337−
13381359instance ToJSON Reference where
13391360 toJSON (Reference ref) = object [ "$ref" .= ref ]
13401361
@@ -1379,20 +1400,10 @@ instance ToJSON AdditionalProperties where
13791400-- Manual FromJSON instances
13801401-- =======================================================================
13811402
1382−instance FromJSON OAuth2Flow where
1383− parseJSON (Object o) = do
1384− (flow :: Text) <- o .: "flow"
1385− case flow of
1386− "implicit" -> OAuth2Implicit <$> o .: "authorizationUrl"
1387− "password" -> OAuth2Password <$> o .: "tokenUrl"
1388− "application" -> OAuth2Application <$> o .: "tokenUrl"
1389− "accessCode" -> OAuth2AccessCode
1390− <$> o .: "authorizationUrl"
1391− <*> o .: "tokenUrl"
1392− _ -> empty
1393− parseJSON _ = empty
1403+instance (Eq p, FromJSON p, AesonDefaultValue p) => FromJSON (OAuth2Flow p) where
1404+ parseJSON = sopSwaggerGenericParseJSON
13941405
1395−instance FromJSON OAuth2Params where
1406+instance FromJSON OAuth2Flows where
13961407 parseJSON = sopSwaggerGenericParseJSON
13971408
13981409instance FromJSON SecuritySchemeType where
@@ -1402,6 +1413,7 @@ instance FromJSON SecuritySchemeType where
14021413 "http" -> pure SecuritySchemeHttp
14031414 "apiKey" -> SecuritySchemeApiKey <$> parseJSON js
14041415 "oauth2" -> SecuritySchemeOAuth2 <$> parseJSON js
1416+ "openIdConnect" -> SecuritySchemeOpenIdConnect <$> (o .: "openIdConnectUrl")
14051417 _ -> empty
14061418 parseJSON _ = empty
14071419
@@ -1509,9 +1521,6 @@ instance FromJSON MediaTypeObject where
15091521instance FromJSON Encoding where
15101522 parseJSON = sopSwaggerGenericParseJSON
15111523
1512−instance FromJSON SecurityDefinitions where
1513− parseJSON js = SecurityDefinitions <$> parseJSON js
1514−
15151524instance FromJSON Reference where
15161525 parseJSON (Object o) = Reference <$> o .: "$ref"
15171526 parseJSON _ = empty
@@ -1573,8 +1582,10 @@ instance HasSwaggerAesonOptions Components where
15731582 swaggerAesonOptions _ = mkSwaggerAesonOptions "components"
15741583instance HasSwaggerAesonOptions Header where
15751584 swaggerAesonOptions _ = mkSwaggerAesonOptions "header" & saoSubObject ?~ "paramSchema"
1576−instance HasSwaggerAesonOptions OAuth2Params where
1577− swaggerAesonOptions _ = mkSwaggerAesonOptions "oauth2" & saoSubObject ?~ "flow"
1585+instance AesonDefaultValue p => HasSwaggerAesonOptions (OAuth2Flow p) where
1586+ swaggerAesonOptions _ = mkSwaggerAesonOptions "oauth2" & saoSubObject ?~ "params"
1587+instance HasSwaggerAesonOptions OAuth2Flows where
1588+ swaggerAesonOptions _ = mkSwaggerAesonOptions "oauth2Flows"
15781589instance HasSwaggerAesonOptions Operation where
15791590 swaggerAesonOptions _ = mkSwaggerAesonOptions "operation"
15801591instance HasSwaggerAesonOptions Param where
@@ -1612,7 +1623,11 @@ instance HasSwaggerAesonOptions (ParamSchema 'SwaggerKindSchema) where
16121623
16131624instance AesonDefaultValue Components
16141625instance AesonDefaultValue (ParamSchema s)
1615−instance AesonDefaultValue OAuth2Flow
1626+instance AesonDefaultValue OAuth2ImplicitFlow
1627+instance AesonDefaultValue OAuth2PasswordFlow
1628+instance AesonDefaultValue OAuth2ClientCredentialsFlow
1629+instance AesonDefaultValue OAuth2AuthorizationCodeFlow
1630+instance AesonDefaultValue p => AesonDefaultValue (OAuth2Flow p)
16161631instance AesonDefaultValue Responses
16171632instance AesonDefaultValue ParamAnySchema
16181633instance AesonDefaultValue SecuritySchemeType
@@ -1620,4 +1635,3 @@ instance AesonDefaultValue (SwaggerType a)
16201635instance AesonDefaultValue MimeList where defaultValue = Just mempty
16211636instance AesonDefaultValue Info
16221637instance AesonDefaultValue ParamLocation
1623−instance AesonDefaultValue SecurityDefinitions where defaultValue = Just $ SecurityDefinitions mempty
src/Data/Swagger/Lens.hs+6−7
@@ -51,7 +51,12 @@ makeLensesWith swaggerFieldRules ''Responses
5151makeFields ''Response
5252makeLensesWith swaggerFieldRules ''SecurityScheme
5353makeFields ''ApiKeyParams
54−makeFields ''OAuth2Params
54+makeFields ''OAuth2ImplicitFlow
55+makeFields ''OAuth2PasswordFlow
56+makeFields ''OAuth2ClientCredentialsFlow
57+makeFields ''OAuth2AuthorizationCodeFlow
58+makeFields ''OAuth2Flow
59+makeFields ''OAuth2Flows
5560makeFields ''ExternalDocs
5661makeFields ''Encoding
5762makeFields ''Example
@@ -103,12 +108,6 @@ instance At Responses where at n = responses . at n
103108instance Ixed Operation where ix n = responses . ix n
104109instance At Operation where at n = responses . at n
105110
106−type instance Index SecurityDefinitions = Text
107−type instance IxValue SecurityDefinitions = SecurityScheme
108−
109−instance Ixed SecurityDefinitions where ix n = (coerced :: Lens' SecurityDefinitions (Definitions SecurityScheme)). ix n
110−instance At SecurityDefinitions where at n = (coerced :: Lens' SecurityDefinitions (Definitions SecurityScheme)). at n
111−
112111instance HasParamSchema NamedSchema (ParamSchema 'SwaggerKindSchema) where paramSchema = schema.paramSchema
113112
114113-- HasType instances
src/Data/Swagger/Optics.hs+6−1
@@ -98,7 +98,12 @@ makeFieldLabels ''Responses
9898makeFieldLabels ''Response
9999makeFieldLabels ''SecurityScheme
100100makeFieldLabels ''ApiKeyParams
101−makeFieldLabels ''OAuth2Params
101+makeFieldLabels ''OAuth2ImplicitFlow
102+makeFieldLabels ''OAuth2PasswordFlow
103+makeFieldLabels ''OAuth2ClientCredentialsFlow
104+makeFieldLabels ''OAuth2AuthorizationCodeFlow
105+makeFieldLabels ''OAuth2Flow
106+makeFieldLabels ''OAuth2Flows
102107makeFieldLabels ''ExternalDocs
103108makeFieldLabels ''Encoding
104109makeFieldLabels ''Example