-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ms-auth and ms-azure-api new release
- Loading branch information
Marco Zocca
committed
Aug 11, 2023
1 parent
9774e2b
commit d09e789
Showing
7 changed files
with
105 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,76 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# language OverloadedStrings #-} | ||
{-# language QuasiQuotes #-} | ||
{-# language ScopedTypeVariables #-} | ||
{-# options_ghc -Wno-unused-imports #-} | ||
module Main (main) where | ||
|
||
import Control.Monad.IO.Class (MonadIO(..)) | ||
-- aeson | ||
import Data.Aeson (eitherDecode) | ||
-- dotenv-micro | ||
import DotEnv.Micro (loadDotEnv) | ||
-- hoauth2 | ||
import Network.OAuth.OAuth2 (OAuth2Token(..)) | ||
import Network.OAuth.OAuth2 (OAuth2Token(..), AccessToken) | ||
import Network.OAuth2.Experiment (IdpApplication, GrantTypeFlow(..)) | ||
-- http-client | ||
import Network.HTTP.Client (Manager) | ||
-- http-types | ||
import Network.HTTP.Types.Status (status200) | ||
-- req | ||
import Network.HTTP.Req (HttpConfig) | ||
-- scotty | ||
import Web.Scotty.Trans (ScottyT, scottyT, get, post, json, text, html, jsonData, status, raise, RoutePattern, middleware) | ||
-- text | ||
import qualified Data.Text.Lazy as TL (Text, pack) | ||
-- transformers | ||
import Control.Monad.Trans.Reader (runReaderT) | ||
-- unliftio | ||
import UnliftIO (MonadUnliftIO(..)) | ||
import UnliftIO.Exception (throwIO) | ||
import UnliftIO.STM (STM, TVar, atomically, newTVarIO, readTVar, writeTVar, modifyTVar) | ||
-- uri-bytestring | ||
import URI.ByteString.QQ (uri) | ||
-- wai-extra | ||
import Network.Wai.Middleware.RequestLogger (logStdoutDev) | ||
|
||
import Network.OAuth2.Provider.AzureAD (azureBotFrameworkADApp, AzureBotFramework) | ||
import Network.OAuth2.Provider.AzureAD (azureBotFrameworkADApp, AzureAD) | ||
import MSAuth (Token, tokenUpdateLoop, readToken, UserSub, Scotty, Action) | ||
import MSAzureAPI (tryReq) | ||
import MSAzureAPI (tryReq, run, withTLS) | ||
import MSAzureAPI.BotService (Activity, sendReply) | ||
|
||
main :: IO () | ||
main = pure () | ||
main = server | ||
|
||
server :: MonadIO m => m () | ||
server = do | ||
loadDotEnv Nothing | ||
ip <- idpApp | ||
withTLS $ \httpcfg mgr -> do | ||
tv <- tokenUpdateLoop ip mgr | ||
let | ||
runR r = runReaderT r tv | ||
scottyT 3000 runR $ do | ||
middleware logStdoutDev | ||
pong tv httpcfg "/pong" | ||
|
||
|
||
pong :: (MonadIO m) => | ||
Token OAuth2Token | ||
-> HttpConfig -> RoutePattern -> Scotty m () | ||
pong tv hc pth = post pth $ do | ||
(acti :: Activity) <- jsonData | ||
m <- (fmap accessToken) <$> readToken tv | ||
case m of | ||
Nothing -> raise "readToken: found Nothing" | ||
Just atok -> do | ||
ei <- run hc $ sendReply acti "It worked!" [] atok | ||
case ei of | ||
Right _ -> status status200 | ||
Left e -> raise $ TL.pack (show e) | ||
|
||
|
||
-- also double check https://stackoverflow.com/a/63929994/2890063 in the AAD app manifest | ||
idpApp :: MonadIO m => m (IdpApplication 'ClientCredentials AzureBotFramework) | ||
idpApp :: MonadIO m => m (IdpApplication 'ClientCredentials AzureAD) | ||
idpApp = azureBotFrameworkADApp "ms-azure-bot-framework-api-test" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters