diff --git a/.gitignore b/.gitignore index 966e62c..e14f969 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /.cabal-sandbox/ /cabal.sandbox.config /.stack-work/ +.DS_Store \ No newline at end of file diff --git a/ms-auth/CHANGELOG.md b/ms-auth/CHANGELOG.md new file mode 100644 index 0000000..57b1356 --- /dev/null +++ b/ms-auth/CHANGELOG.md @@ -0,0 +1,11 @@ +# Changelog for `ms-auth` + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to the +[Haskell Package Versioning Policy](https://pvp.haskell.org/). + +## Unreleased + +## 0.1.0.0 - YYYY-MM-DD diff --git a/LICENSE b/ms-auth/LICENSE similarity index 100% rename from LICENSE rename to ms-auth/LICENSE diff --git a/Setup.hs b/ms-auth/Setup.hs similarity index 100% rename from Setup.hs rename to ms-auth/Setup.hs diff --git a/ms-auth/ms-auth.cabal b/ms-auth/ms-auth.cabal new file mode 100644 index 0000000..dd2d082 --- /dev/null +++ b/ms-auth/ms-auth.cabal @@ -0,0 +1,52 @@ +name: ms-auth +version: 0.1.0.0 +synopsis: Microsoft Authentication API +description: Bindings to the Microsoft Authentication API / Azure ActiveDirectory (AAD) for building applications that use either Delegated or App-only permissions. Helper functions are provided for building OAuth2 authentication flows and keep tokens transactionally secure and up to date. +homepage: https://github.com/unfoldml/ms-auth +license: BSD3 +license-file: LICENSE +author: Marco Zocca +maintainer: oss@unfoldml.com +copyright: 2023 Marco Zocca, UnfoldML +category: Web +build-type: Simple +extra-source-files: README.md + CHANGELOG.md +cabal-version: >=1.10 +tested-with: GHC == 9.2.8 + +library + default-language: Haskell2010 + hs-source-dirs: src + exposed-modules: Network.OAuth2.JWT + Network.OAuth2.Session + Network.OAuth2.Provider.AzureAD + build-depends: base >= 4.7 && < 5 + , aeson + , bytestring + , containers + , hoauth2 == 2.6.0 + , http-client + , http-types + , jwt + , scientific + , scotty + , text + , time + , transformers + , uri-bytestring + , validation-micro + , unliftio + ghc-options: -Wall + -Wcompat + -Widentities + -Wincomplete-record-updates + -Wincomplete-uni-patterns + -Wmissing-export-lists + -Wmissing-home-modules + -Wpartial-fields + -Wredundant-constraints + +source-repository head + type: git + location: https://github.com/unfoldml/ms-auth diff --git a/src/Network/OAuth2/JWT.hs b/ms-auth/src/Network/OAuth2/JWT.hs similarity index 100% rename from src/Network/OAuth2/JWT.hs rename to ms-auth/src/Network/OAuth2/JWT.hs diff --git a/src/Network/OAuth2/Provider/AzureAD.hs b/ms-auth/src/Network/OAuth2/Provider/AzureAD.hs similarity index 100% rename from src/Network/OAuth2/Provider/AzureAD.hs rename to ms-auth/src/Network/OAuth2/Provider/AzureAD.hs diff --git a/src/Network/OAuth2/Session.hs b/ms-auth/src/Network/OAuth2/Session.hs similarity index 100% rename from src/Network/OAuth2/Session.hs rename to ms-auth/src/Network/OAuth2/Session.hs diff --git a/ms-auth/stack.yaml b/ms-auth/stack.yaml new file mode 100644 index 0000000..610bc09 --- /dev/null +++ b/ms-auth/stack.yaml @@ -0,0 +1,36 @@ +resolver: + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/26.yaml + +packages: +- . + +extra-deps: +- validation-micro-1.0.0.0 +# - git: https://github.com/commercialhaskell/stack.git +# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a + +# extra-deps: [] + +# Override default flag values for local packages and extra-deps +# flags: {} + +# Extra package databases containing global packages +# extra-package-dbs: [] + +# Control whether we use the GHC we find on the path +# system-ghc: true +# +# Require a specific version of Stack, using version ranges +# require-stack-version: -any # Default +# require-stack-version: ">=2.9" +# +# Override the architecture used by Stack, especially useful on Windows +# arch: i386 +# arch: x86_64 +# +# Extra directories used by Stack for building +# extra-include-dirs: [/path/to/dir] +# extra-lib-dirs: [/path/to/dir] +# +# Allow a newer minor version of GHC than the snapshot specifies +# compiler-check: newer-minor diff --git a/test/Spec.hs b/ms-auth/test/Spec.hs similarity index 100% rename from test/Spec.hs rename to ms-auth/test/Spec.hs diff --git a/ms-azure-api/CHANGELOG.md b/ms-azure-api/CHANGELOG.md new file mode 100644 index 0000000..8f5f1df --- /dev/null +++ b/ms-azure-api/CHANGELOG.md @@ -0,0 +1,11 @@ +# Changelog for `ms-azure-api` + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to the +[Haskell Package Versioning Policy](https://pvp.haskell.org/). + +## Unreleased + +## 0.1.0.0 - YYYY-MM-DD diff --git a/ms-azure-api/LICENSE b/ms-azure-api/LICENSE new file mode 100644 index 0000000..c5b6c16 --- /dev/null +++ b/ms-azure-api/LICENSE @@ -0,0 +1,30 @@ +Copyright Author name here (c) 2023 + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of Author name here nor the names of other + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/ms-azure-api/README.md b/ms-azure-api/README.md new file mode 100644 index 0000000..29c011a --- /dev/null +++ b/ms-azure-api/README.md @@ -0,0 +1,5 @@ +# ms-azure-api + +[![Build Status](https://travis-ci.org/unfoldml/ms-azure-api.png)](https://travis-ci.org/unfoldml/ms-azure-api) + +TODO Description. diff --git a/ms-azure-api/Setup.hs b/ms-azure-api/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/ms-azure-api/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/ms-azure-api/ms-azure-api.cabal b/ms-azure-api/ms-azure-api.cabal new file mode 100644 index 0000000..aefc77a --- /dev/null +++ b/ms-azure-api/ms-azure-api.cabal @@ -0,0 +1,47 @@ +name: ms-azure-api +version: 0.1.0.0 +-- synopsis: +-- description: +homepage: https://github.com/unfoldml/ms-azure-api +license: BSD3 +license-file: LICENSE +author: Marco Zocca +maintainer: oss@unfoldml.com +copyright: 2023 Marco Zocca +category: Web +build-type: Simple +extra-source-files: README.md + CHANGELOG.md +cabal-version: >=1.10 +tested-with: GHC == 7.10.2 + +library + default-language: Haskell2010 + hs-source-dirs: src + exposed-modules: MSAzureAPI.Internal.Common + build-depends: base >= 4.7 && < 5 + , aeson + , bytestring + , containers + , hoauth2 == 2.6.0 + , http-types + , modern-uri + , req + , scientific + , text + , time >= 1.8 + , transformers >= 0.5 + , unliftio + ghc-options: -Wall + -Wcompat + -Wno-unused-imports + default-extensions: OverloadedStrings + DeriveGeneric + DeriveFunctor + DerivingStrategies + LambdaCase + + +source-repository head + type: git + location: https://github.com/unfoldml/ms-azure-api diff --git a/ms-azure-api/src/MSAzureAPI.hs b/ms-azure-api/src/MSAzureAPI.hs new file mode 100644 index 0000000..68a29ed --- /dev/null +++ b/ms-azure-api/src/MSAzureAPI.hs @@ -0,0 +1 @@ +module MSAzureAPI where diff --git a/ms-azure-api/src/MSAzureAPI/Internal/Common.hs b/ms-azure-api/src/MSAzureAPI/Internal/Common.hs new file mode 100644 index 0000000..ca1b340 --- /dev/null +++ b/ms-azure-api/src/MSAzureAPI/Internal/Common.hs @@ -0,0 +1,78 @@ +{-# LANGUAGE DataKinds #-} +{-# options_ghc -Wno-unused-imports #-} +-- | Common functions for the MS Azure API +-- +module MSAzureAPI.Internal.Common where + +import Control.Monad.IO.Class (MonadIO(..)) +import Data.Proxy (Proxy) +import GHC.Generics (Generic(..)) + +import Data.List (sort, sortBy, stripPrefix, uncons) +import Data.Maybe (listToMaybe, fromMaybe) +-- import Data.Ord (comparing) +import Data.Char (toLower) + +-- aeson +import qualified Data.Aeson as A (ToJSON(..), FromJSON(..), genericParseJSON, defaultOptions, Options(..), withObject, withText, (.:), (.:?), object, (.=), Key, Value, camelTo2) +-- bytestring +import qualified Data.ByteString as BS (ByteString) +import qualified Data.ByteString.Char8 as BS8 (pack, unpack) +import qualified Data.ByteString.Lazy as LBS (ByteString) +import qualified Data.ByteString.Lazy.Char8 as LBS8 (pack, unpack, putStrLn) +-- hoauth2 +import Network.OAuth.OAuth2 (OAuth2Token(..)) +import Network.OAuth.OAuth2.Internal (AccessToken(..), ExchangeToken(..), RefreshToken(..), OAuth2Error, IdToken(..)) +-- modern-uri +-- import Text.URI (URI, mkURI) +-- req +import Network.HTTP.Req (Req, runReq, HttpException(..), defaultHttpConfig, req, Option, (=:), GET(..), POST(..), Url, Scheme(..), useHttpsURI, https, (/:), ReqBodyJson(..), NoReqBody(..), oAuth2Bearer, HttpResponse(..), jsonResponse, JsonResponse, lbsResponse, LbsResponse, bsResponse, BsResponse, responseBody) +-- text +import Data.Text (Text, pack, unpack) + + + +-- | Data plane e.g for FileREST API + +-- | Control plane +msAzureManagementReqConfig :: AccessToken -> [Text] -> (Url 'Https, Option 'Https) +msAzureManagementReqConfig (AccessToken ttok) uriRest = (url, os) + where + url = (https "management.azure.com") //: uriRest + os = oAuth2Bearer $ BS8.pack (unpack ttok) + +(//:) :: Url scheme -> [Text] -> Url scheme +(//:) = foldl (/:) + + +-- * aeson + +-- | a collection of items with key @value@ +data Collection a = Collection { + cValue :: [a] + } deriving (Eq, Show, Generic) +instance A.FromJSON a => A.FromJSON (Collection a) where + parseJSON = A.genericParseJSON (aesonOptions "c") + +-- | drop the prefix and lowercase first character +-- +-- e.g. @userDisplayName@ @->@ @displayName@ +aesonOptions :: String -- ^ record prefix + -> A.Options +aesonOptions pfx = A.defaultOptions { A.fieldLabelModifier = recordName pfx } + +-- | drop the prefix and lowercase first character +recordName :: String -- ^ record name prefix + -> String -- ^ JSON field name + -> String +recordName pf str = case uncons $ dropPrefix pf str of + Just (c, cs) -> toLower c : cs + _ -> error "record name cannot be empty" + +-- | Drops the given prefix from a list. +-- It returns the original sequence if the sequence doesn't start with the given prefix. +-- +-- > dropPrefix "Mr. " "Mr. Men" == "Men" +-- > dropPrefix "Mr. " "Dr. Men" == "Dr. Men" +dropPrefix :: Eq a => [a] -> [a] -> [a] +dropPrefix a b = fromMaybe b $ stripPrefix a b diff --git a/ms-azure-api/stack.yaml b/ms-azure-api/stack.yaml new file mode 100644 index 0000000..4882c9f --- /dev/null +++ b/ms-azure-api/stack.yaml @@ -0,0 +1,39 @@ +resolver: + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/26.yaml + +packages: +- . +# Dependency packages to be pulled from upstream that are not in the resolver. +# These entries can reference officially published versions as well as +# forks / in-progress versions pinned to a git hash. For example: +# +# extra-deps: +# - acme-missiles-0.3 +# - git: https://github.com/commercialhaskell/stack.git +# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a +# +# extra-deps: [] + +# Override default flag values for local packages and extra-deps +# flags: {} + +# Extra package databases containing global packages +# extra-package-dbs: [] + +# Control whether we use the GHC we find on the path +# system-ghc: true +# +# Require a specific version of Stack, using version ranges +# require-stack-version: -any # Default +# require-stack-version: ">=2.9" +# +# Override the architecture used by Stack, especially useful on Windows +# arch: i386 +# arch: x86_64 +# +# Extra directories used by Stack for building +# extra-include-dirs: [/path/to/dir] +# extra-lib-dirs: [/path/to/dir] +# +# Allow a newer minor version of GHC than the snapshot specifies +# compiler-check: newer-minor diff --git a/ms-azure-api/test/Spec.hs b/ms-azure-api/test/Spec.hs new file mode 100644 index 0000000..a824f8c --- /dev/null +++ b/ms-azure-api/test/Spec.hs @@ -0,0 +1 @@ +{-# OPTIONS_GHC -F -pgmF hspec-discover #-} diff --git a/CHANGELOG.md b/ms-graph-api/CHANGELOG.md similarity index 77% rename from CHANGELOG.md rename to ms-graph-api/CHANGELOG.md index 4f905dd..fef261b 100644 --- a/CHANGELOG.md +++ b/ms-graph-api/CHANGELOG.md @@ -8,8 +8,14 @@ and this project adheres to the ## Unreleased -MSGraphAPI.ChangeNotifications.Subscription : +## 1.0.0.0 + +MSGraphAPI.ChangeNotifications.Subscription + +== Breaking changes: + +Moved the Network/* module hierarchy to the @ms-auth@ package shared with @ms-azure-api@ ## 0.6.0.0 diff --git a/ms-graph-api/LICENSE b/ms-graph-api/LICENSE new file mode 100644 index 0000000..c5b6c16 --- /dev/null +++ b/ms-graph-api/LICENSE @@ -0,0 +1,30 @@ +Copyright Author name here (c) 2023 + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of Author name here nor the names of other + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/ms-graph-api/README.md similarity index 100% rename from README.md rename to ms-graph-api/README.md diff --git a/ms-graph-api/Setup.hs b/ms-graph-api/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/ms-graph-api/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/ms-graph-api.cabal b/ms-graph-api/ms-graph-api.cabal similarity index 84% rename from ms-graph-api.cabal rename to ms-graph-api/ms-graph-api.cabal index d307377..bb0598a 100644 --- a/ms-graph-api.cabal +++ b/ms-graph-api/ms-graph-api.cabal @@ -1,5 +1,5 @@ name: ms-graph-api -version: 0.6.0.0 +version: 1.0.0.0 synopsis: Microsoft Graph API description: Bindings to the Microsoft Graph API homepage: https://github.com/unfoldml/ms-graph-api @@ -24,32 +24,21 @@ library MSGraphAPI.Users.Group MSGraphAPI.Drive MSGraphAPI.Files.DriveItem - Network.OAuth2.Provider.AzureAD - Network.OAuth2.Session MSGraphAPI.Internal.Common - other-modules: - MSGraphAPI.Auth - Network.OAuth2.JWT + other-modules: MSGraphAPI.Auth build-depends: base >= 4.7 && < 5 , aeson , bytestring , containers , hoauth2 == 2.6.0 - , http-client , http-types - , jwt , modern-uri , req , scientific - , scotty , text , time >= 1.8 , transformers >= 0.5 , unliftio - , uri-bytestring - , validation-micro - , wai - , warp ghc-options: -Wall -Wcompat -Wno-unused-imports diff --git a/src/MSGraphAPI/Auth.hs b/ms-graph-api/src/MSGraphAPI/Auth.hs similarity index 100% rename from src/MSGraphAPI/Auth.hs rename to ms-graph-api/src/MSGraphAPI/Auth.hs diff --git a/src/MSGraphAPI/ChangeNotifications/Subscription.hs b/ms-graph-api/src/MSGraphAPI/ChangeNotifications/Subscription.hs similarity index 100% rename from src/MSGraphAPI/ChangeNotifications/Subscription.hs rename to ms-graph-api/src/MSGraphAPI/ChangeNotifications/Subscription.hs diff --git a/src/MSGraphAPI/Drive.hs b/ms-graph-api/src/MSGraphAPI/Drive.hs similarity index 100% rename from src/MSGraphAPI/Drive.hs rename to ms-graph-api/src/MSGraphAPI/Drive.hs diff --git a/src/MSGraphAPI/Files/DriveItem.hs b/ms-graph-api/src/MSGraphAPI/Files/DriveItem.hs similarity index 100% rename from src/MSGraphAPI/Files/DriveItem.hs rename to ms-graph-api/src/MSGraphAPI/Files/DriveItem.hs diff --git a/src/MSGraphAPI/Internal/Common.hs b/ms-graph-api/src/MSGraphAPI/Internal/Common.hs similarity index 99% rename from src/MSGraphAPI/Internal/Common.hs rename to ms-graph-api/src/MSGraphAPI/Internal/Common.hs index 703834b..5d2c464 100644 --- a/src/MSGraphAPI/Internal/Common.hs +++ b/ms-graph-api/src/MSGraphAPI/Internal/Common.hs @@ -50,7 +50,6 @@ import Data.Text (Text, pack, unpack) import UnliftIO (MonadUnliftIO(..)) import UnliftIO.Exception (try) -import Network.OAuth2.Session (Tokens) -- | Specialized version of 'try' to 'HttpException's diff --git a/src/MSGraphAPI/User.hs b/ms-graph-api/src/MSGraphAPI/User.hs similarity index 100% rename from src/MSGraphAPI/User.hs rename to ms-graph-api/src/MSGraphAPI/User.hs diff --git a/src/MSGraphAPI/Users/Group.hs b/ms-graph-api/src/MSGraphAPI/Users/Group.hs similarity index 100% rename from src/MSGraphAPI/Users/Group.hs rename to ms-graph-api/src/MSGraphAPI/Users/Group.hs diff --git a/src/MSGraphAPI/Users/User.hs b/ms-graph-api/src/MSGraphAPI/Users/User.hs similarity index 100% rename from src/MSGraphAPI/Users/User.hs rename to ms-graph-api/src/MSGraphAPI/Users/User.hs diff --git a/stack.yaml b/ms-graph-api/stack.yaml similarity index 100% rename from stack.yaml rename to ms-graph-api/stack.yaml diff --git a/ms-graph-api/test/Spec.hs b/ms-graph-api/test/Spec.hs new file mode 100644 index 0000000..a824f8c --- /dev/null +++ b/ms-graph-api/test/Spec.hs @@ -0,0 +1 @@ +{-# OPTIONS_GHC -F -pgmF hspec-discover #-}