git.iko.soy/typeable/openapi3
on master help sign in
Migrate OAuth2 flows
fec285bMaxim Koltsov
src/Data/Swagger.hs+5−2
@@ -95,15 +95,18 @@ module Data.Swagger (
95 SecurityScheme(..),
95 SecurityScheme(..),
96 SecuritySchemeType(..),
96 SecuritySchemeType(..),
97 SecurityRequirement(..),
97 SecurityRequirement(..),
98− SecurityDefinitions(..),
99
98
100 -- *** API key
99 -- *** API key
101 ApiKeyParams(..),
100 ApiKeyParams(..),
102 ApiKeyLocation(..),
101 ApiKeyLocation(..),
103
102
104 -- *** OAuth2
103 -- *** OAuth2
105− OAuth2Params(..),
104+ OAuth2Flows(..),
106 OAuth2Flow(..),
105 OAuth2Flow(..),
106+ OAuth2ImplicitFlow(..),
107+ OAuth2PasswordFlow(..),
108+ OAuth2ClientCredentialsFlow(..),
109+ OAuth2AuthorizationCodeFlow(..),
107 AuthorizationURL,
110 AuthorizationURL,
108 TokenURL,
111 TokenURL,
109
112
src/Data/Swagger/Internal.hs+92−78
@@ -838,26 +838,54 @@ type AuthorizationURL = Text
838-- | The token URL to be used for OAuth2 flow. This SHOULD be in the form of a URL.
838-- | The token URL to be used for OAuth2 flow. This SHOULD be in the form of a URL.
839type TokenURL = Text
839type TokenURL = Text
840
840
841−data OAuth2Flow
841+data OAuth2ImplicitFlow = OAuth2ImplicitFlow
842− = OAuth2Implicit AuthorizationURL
842+ { _oAuth2ImplicitFlowAuthorizationUrl :: AuthorizationURL
843− | OAuth2Password TokenURL
843+ } deriving (Eq, Show, Generic, Data, Typeable)
844− | OAuth2Application TokenURL
844+
845− | OAuth2AccessCode AuthorizationURL TokenURL
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
846− deriving (Eq, Show, Generic, Data, Typeable)
856+ } deriving (Eq, Show, Generic, Data, Typeable)
847
857
848−data OAuth2Params = OAuth2Params
858+data OAuth2Flow p = OAuth2Flow
849− { -- | The flow used by the OAuth2 security scheme.
859+ { _oAuth2Params :: p
850− _oauth2Flow :: OAuth2Flow
860+
861+ -- | The URL to be used for obtaining refresh tokens.
862+ , _oAath2RefreshUrl :: Maybe URL
851
863
852 -- | The available scopes for the OAuth2 security scheme.
864 -- | The available scopes for the OAuth2 security scheme.
865+ -- A map between the scope name and a short description for it.
866+ -- The map MAY be empty.
853− , _oauth2Scopes :: InsOrdHashMap Text Text
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)
854 } deriving (Eq, Show, Generic, Data, Typeable)
882 } deriving (Eq, Show, Generic, Data, Typeable)
855
883
856data SecuritySchemeType
884data SecuritySchemeType
857 = SecuritySchemeHttp
885 = SecuritySchemeHttp
858 | SecuritySchemeApiKey ApiKeyParams
886 | SecuritySchemeApiKey ApiKeyParams
859− | SecuritySchemeOAuth2 OAuth2Params
887+ | SecuritySchemeOAuth2 OAuth2Flows
860−-- | SecuritySchemeOpenIdConnect -- FIXME
888+ | SecuritySchemeOpenIdConnect URL
861 deriving (Eq, Show, Generic, Data, Typeable)
889 deriving (Eq, Show, Generic, Data, Typeable)
862
890
863data SecurityScheme = SecurityScheme
891data SecurityScheme = SecurityScheme
@@ -868,22 +896,6 @@ data SecurityScheme = SecurityScheme
868 , _securitySchemeDescription :: Maybe Text
896 , _securitySchemeDescription :: Maybe Text
869 } deriving (Eq, Show, Generic, Data, Typeable)
897 } deriving (Eq, Show, Generic, Data, Typeable)
870
898
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−
887-- | Lists the required security schemes to execute this operation.
899-- | Lists the required security schemes to execute this operation.
888-- The object can have multiple security schemes declared in it which are all required
900-- The object can have multiple security schemes declared in it which are all required
889-- (that is, there is a logical AND between the schemes).
901-- (that is, there is a logical AND between the schemes).
@@ -951,7 +963,8 @@ data AdditionalProperties
951
963
952deriveGeneric ''Components
964deriveGeneric ''Components
953deriveGeneric ''Header
965deriveGeneric ''Header
954−deriveGeneric ''OAuth2Params
966+deriveGeneric ''OAuth2Flow
967+deriveGeneric ''OAuth2Flows
955deriveGeneric ''Operation
968deriveGeneric ''Operation
956deriveGeneric ''Param
969deriveGeneric ''Param
957deriveGeneric ''ParamOtherSchema
970deriveGeneric ''ParamOtherSchema
@@ -1067,15 +1080,10 @@ instance Monoid Operation where
1067 mempty = genericMempty
1080 mempty = genericMempty
1068 mappend = (<>)
1081 mappend = (<>)
1069
1082
1070−instance Semigroup SecurityScheme where
1083+instance Semigroup OAuth2Flows where
1071− (<>) = mergeSecurityScheme
1084+ (<>) = genericMappend
1072−
1073−instance Semigroup SecurityDefinitions where
1074− (SecurityDefinitions sd1) <> (SecurityDefinitions sd2) =
1075− SecurityDefinitions $ InsOrdHashMap.unionWith (<>) sd1 sd2
1076−
1077−instance Monoid SecurityDefinitions where
1085+instance Monoid OAuth2Flows where
1078− mempty = SecurityDefinitions InsOrdHashMap.empty
1086+ mempty = genericMempty
1079 mappend = (<>)
1087 mappend = (<>)
1080
1088
1081-- =======================================================================
1089-- =======================================================================
@@ -1093,7 +1101,6 @@ instance SwaggerMonoid Responses
1093instance SwaggerMonoid Response
1101instance SwaggerMonoid Response
1094instance SwaggerMonoid ExternalDocs
1102instance SwaggerMonoid ExternalDocs
1095instance SwaggerMonoid Operation
1103instance SwaggerMonoid Operation
1096−instance SwaggerMonoid SecurityDefinitions
1097instance (Eq a, Hashable a) => SwaggerMonoid (InsOrdHashSet a)
1104instance (Eq a, Hashable a) => SwaggerMonoid (InsOrdHashSet a)
1098
1105
1099instance SwaggerMonoid MimeList
1106instance SwaggerMonoid MimeList
@@ -1165,6 +1172,18 @@ instance ToJSON Xml where
1165instance ToJSON Discriminator where
1172instance ToJSON Discriminator where
1166 toJSON = genericToJSON (jsonPrefix "Discriminator")
1173 toJSON = genericToJSON (jsonPrefix "Discriminator")
1167
1174
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+
1168-- =======================================================================
1187-- =======================================================================
1169-- Simple Generic-based FromJSON instances
1188-- Simple Generic-based FromJSON instances
1170-- =======================================================================
1189-- =======================================================================
@@ -1205,26 +1224,27 @@ instance FromJSON ExternalDocs where
1205instance FromJSON Discriminator where
1224instance FromJSON Discriminator where
1206 parseJSON = genericParseJSON (jsonPrefix "Discriminator")
1225 parseJSON = genericParseJSON (jsonPrefix "Discriminator")
1207
1226
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+
1208-- =======================================================================
1239-- =======================================================================
1209-- Manual ToJSON instances
1240-- Manual ToJSON instances
1210-- =======================================================================
1241-- =======================================================================
1211
1242
1212−instance ToJSON OAuth2Flow where
1243+instance (Eq p, ToJSON p, AesonDefaultValue p) =>ToJSON (OAuth2Flow p) where
1213− toJSON (OAuth2Implicit authUrl) = object
1244+ toJSON = sopSwaggerGenericToJSON
1214− [ "flow" .= ("implicit" :: Text)
1245+ toEncoding = sopSwaggerGenericToEncoding
1215− , "authorizationUrl" .= authUrl ]
1246+
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
1247+instance ToJSON OAuth2Flows where
1228 toJSON = sopSwaggerGenericToJSON
1248 toJSON = sopSwaggerGenericToJSON
1229 toEncoding = sopSwaggerGenericToEncoding
1249 toEncoding = sopSwaggerGenericToEncoding
1230
1250
@@ -1237,6 +1257,10 @@ instance ToJSON SecuritySchemeType where
1237 toJSON (SecuritySchemeOAuth2 params)
1257 toJSON (SecuritySchemeOAuth2 params)
1238 = toJSON params
1258 = toJSON params
1239 <+> object [ "type" .= ("oauth2" :: Text) ]
1259 <+> object [ "type" .= ("oauth2" :: Text) ]
1260+ toJSON (SecuritySchemeOpenIdConnect url) = object
1261+ [ "type" .= ("openIdConnect" :: Text)
1262+ , "openIdConnectUrl" .= url
1263+ ]
1240
1264
1241instance ToJSON Swagger where
1265instance ToJSON Swagger where
1242 toJSON a = sopSwaggerGenericToJSON a &
1266 toJSON a = sopSwaggerGenericToJSON a &
@@ -1332,9 +1356,6 @@ instance ToJSON Encoding where
1332 toJSON = sopSwaggerGenericToJSON
1356 toJSON = sopSwaggerGenericToJSON
1333 toEncoding = sopSwaggerGenericToEncoding
1357 toEncoding = sopSwaggerGenericToEncoding
1334
1358
1335−instance ToJSON SecurityDefinitions where
1336− toJSON (SecurityDefinitions sd) = toJSON sd
1337−
1338instance ToJSON Reference where
1359instance ToJSON Reference where
1339 toJSON (Reference ref) = object [ "$ref" .= ref ]
1360 toJSON (Reference ref) = object [ "$ref" .= ref ]
1340
1361
@@ -1379,20 +1400,10 @@ instance ToJSON AdditionalProperties where
1379-- Manual FromJSON instances
1400-- Manual FromJSON instances
1380-- =======================================================================
1401-- =======================================================================
1381
1402
1382−instance FromJSON OAuth2Flow where
1403+instance (Eq p, FromJSON p, AesonDefaultValue p) => FromJSON (OAuth2Flow p) where
1383− parseJSON (Object o) = do
1404+ parseJSON = sopSwaggerGenericParseJSON
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
1394
1405
1395−instance FromJSON OAuth2Params where
1406+instance FromJSON OAuth2Flows where
1396 parseJSON = sopSwaggerGenericParseJSON
1407 parseJSON = sopSwaggerGenericParseJSON
1397
1408
1398instance FromJSON SecuritySchemeType where
1409instance FromJSON SecuritySchemeType where
@@ -1402,6 +1413,7 @@ instance FromJSON SecuritySchemeType where
1402 "http" -> pure SecuritySchemeHttp
1413 "http" -> pure SecuritySchemeHttp
1403 "apiKey" -> SecuritySchemeApiKey <$> parseJSON js
1414 "apiKey" -> SecuritySchemeApiKey <$> parseJSON js
1404 "oauth2" -> SecuritySchemeOAuth2 <$> parseJSON js
1415 "oauth2" -> SecuritySchemeOAuth2 <$> parseJSON js
1416+ "openIdConnect" -> SecuritySchemeOpenIdConnect <$> (o .: "openIdConnectUrl")
1405 _ -> empty
1417 _ -> empty
1406 parseJSON _ = empty
1418 parseJSON _ = empty
1407
1419
@@ -1509,9 +1521,6 @@ instance FromJSON MediaTypeObject where
1509instance FromJSON Encoding where
1521instance FromJSON Encoding where
1510 parseJSON = sopSwaggerGenericParseJSON
1522 parseJSON = sopSwaggerGenericParseJSON
1511
1523
1512−instance FromJSON SecurityDefinitions where
1513− parseJSON js = SecurityDefinitions <$> parseJSON js
1514−
1515instance FromJSON Reference where
1524instance FromJSON Reference where
1516 parseJSON (Object o) = Reference <$> o .: "$ref"
1525 parseJSON (Object o) = Reference <$> o .: "$ref"
1517 parseJSON _ = empty
1526 parseJSON _ = empty
@@ -1573,8 +1582,10 @@ instance HasSwaggerAesonOptions Components where
1573 swaggerAesonOptions _ = mkSwaggerAesonOptions "components"
1582 swaggerAesonOptions _ = mkSwaggerAesonOptions "components"
1574instance HasSwaggerAesonOptions Header where
1583instance HasSwaggerAesonOptions Header where
1575 swaggerAesonOptions _ = mkSwaggerAesonOptions "header" & saoSubObject ?~ "paramSchema"
1584 swaggerAesonOptions _ = mkSwaggerAesonOptions "header" & saoSubObject ?~ "paramSchema"
1576−instance HasSwaggerAesonOptions OAuth2Params where
1585+instance AesonDefaultValue p => HasSwaggerAesonOptions (OAuth2Flow p) where
1577− swaggerAesonOptions _ = mkSwaggerAesonOptions "oauth2" & saoSubObject ?~ "flow"
1586+ swaggerAesonOptions _ = mkSwaggerAesonOptions "oauth2" & saoSubObject ?~ "params"
1587+instance HasSwaggerAesonOptions OAuth2Flows where
1588+ swaggerAesonOptions _ = mkSwaggerAesonOptions "oauth2Flows"
1578instance HasSwaggerAesonOptions Operation where
1589instance HasSwaggerAesonOptions Operation where
1579 swaggerAesonOptions _ = mkSwaggerAesonOptions "operation"
1590 swaggerAesonOptions _ = mkSwaggerAesonOptions "operation"
1580instance HasSwaggerAesonOptions Param where
1591instance HasSwaggerAesonOptions Param where
@@ -1612,7 +1623,11 @@ instance HasSwaggerAesonOptions (ParamSchema 'SwaggerKindSchema) where
1612
1623
1613instance AesonDefaultValue Components
1624instance AesonDefaultValue Components
1614instance AesonDefaultValue (ParamSchema s)
1625instance 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)
1616instance AesonDefaultValue Responses
1631instance AesonDefaultValue Responses
1617instance AesonDefaultValue ParamAnySchema
1632instance AesonDefaultValue ParamAnySchema
1618instance AesonDefaultValue SecuritySchemeType
1633instance AesonDefaultValue SecuritySchemeType
@@ -1620,4 +1635,3 @@ instance AesonDefaultValue (SwaggerType a)
1620instance AesonDefaultValue MimeList where defaultValue = Just mempty
1635instance AesonDefaultValue MimeList where defaultValue = Just mempty
1621instance AesonDefaultValue Info
1636instance AesonDefaultValue Info
1622instance AesonDefaultValue ParamLocation
1637instance 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
51makeFields ''Response
51makeFields ''Response
52makeLensesWith swaggerFieldRules ''SecurityScheme
52makeLensesWith swaggerFieldRules ''SecurityScheme
53makeFields ''ApiKeyParams
53makeFields ''ApiKeyParams
54−makeFields ''OAuth2Params
54+makeFields ''OAuth2ImplicitFlow
55+makeFields ''OAuth2PasswordFlow
56+makeFields ''OAuth2ClientCredentialsFlow
57+makeFields ''OAuth2AuthorizationCodeFlow
58+makeFields ''OAuth2Flow
59+makeFields ''OAuth2Flows
55makeFields ''ExternalDocs
60makeFields ''ExternalDocs
56makeFields ''Encoding
61makeFields ''Encoding
57makeFields ''Example
62makeFields ''Example
@@ -103,12 +108,6 @@ instance At Responses where at n = responses . at n
103instance Ixed Operation where ix n = responses . ix n
108instance Ixed Operation where ix n = responses . ix n
104instance At Operation where at n = responses . at n
109instance At Operation where at n = responses . at n
105
110
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−
112instance HasParamSchema NamedSchema (ParamSchema 'SwaggerKindSchema) where paramSchema = schema.paramSchema
111instance HasParamSchema NamedSchema (ParamSchema 'SwaggerKindSchema) where paramSchema = schema.paramSchema
113
112
114-- HasType instances
113-- HasType instances
src/Data/Swagger/Optics.hs+6−1
@@ -98,7 +98,12 @@ makeFieldLabels ''Responses
98makeFieldLabels ''Response
98makeFieldLabels ''Response
99makeFieldLabels ''SecurityScheme
99makeFieldLabels ''SecurityScheme
100makeFieldLabels ''ApiKeyParams
100makeFieldLabels ''ApiKeyParams
101−makeFieldLabels ''OAuth2Params
101+makeFieldLabels ''OAuth2ImplicitFlow
102+makeFieldLabels ''OAuth2PasswordFlow
103+makeFieldLabels ''OAuth2ClientCredentialsFlow
104+makeFieldLabels ''OAuth2AuthorizationCodeFlow
105+makeFieldLabels ''OAuth2Flow
106+makeFieldLabels ''OAuth2Flows
102makeFieldLabels ''ExternalDocs
107makeFieldLabels ''ExternalDocs
103makeFieldLabels ''Encoding
108makeFieldLabels ''Encoding
104makeFieldLabels ''Example
109makeFieldLabels ''Example