Skip to content

Commit

Permalink
whoami integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Leduc committed May 4, 2016
1 parent 68a6f65 commit 2a754f3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
7 changes: 2 additions & 5 deletions app/Kreg/Options/Auth.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ module Kreg.Options.Auth (auth) where

import Kreg.UI.UserInput (askLogins)
import qualified Registry.Client as C
import System.Directory (getHomeDirectory)
import System.FilePath.Posix ((</>))
import Kreg.TokenFileManager (save)

auth :: IO ()
auth = do
Expand All @@ -15,7 +14,5 @@ auth = do
case res of
Nothing -> putStrLn "Authentication failed"
Just dt -> do
homeDirectory <- getHomeDirectory
writeFile (homeDirectory </> ".kregrch") (C.id_token dt)

save dt
putStrLn "Authentication succeeded"
15 changes: 14 additions & 1 deletion app/Kreg/Options/Whoami.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
module Kreg.Options.Whoami (whoami) where

import qualified Kreg.TokenFileManager as TMF (read)
import qualified Registry.Client as C (whoami)

{-
uri : /api/account
prerequisites :
* token stored in ~/.kregrch
-}
whoami :: IO ()
whoami = undefined
whoami = do
token <- TMF.read
res <- C.whoami token
putStrLn (show res)


-- TODO : JWT libs too young to works on jhipster particular configuration. We'll need to query the json api to have more details.
19 changes: 19 additions & 0 deletions app/Kreg/TokenFileManager.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Kreg.TokenFileManager (save, Kreg.TokenFileManager.read) where

import System.FilePath.Posix ((</>))
import System.Directory (getHomeDirectory)
import qualified Registry.Client as C

filePath = do
homeDirectory <- getHomeDirectory
return $ homeDirectory </> ".kregrch"

--save :: String -> IO ()
save token = do
fp <- filePath
writeFile fp (C.id_token token)

--read :: IO String
read = do
fp <- filePath
readFile fp
1 change: 1 addition & 0 deletions kreg.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ library
, http-client
, aeson
, lens
, bytestring
default-language: Haskell2010

executable kreg-exe
Expand Down
28 changes: 26 additions & 2 deletions src/Registry/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Registry.Client (auth, AuthResponseBody(..)) where
module Registry.Client (auth, whoami, AuthResponseBody(..), AccountResponseBody(..)) where

import Control.Exception as E
import Control.Lens ((^?))
import Control.Lens ((^?), (&), (.~))
import Data.Aeson
import GHC.Generics
import Network.HTTP.Client (HttpException (StatusCodeException))
import qualified Network.Wreq as R
import Data.ByteString.Char8 (pack)

data Auth = Auth {
username :: String,
Expand All @@ -21,11 +22,24 @@ data AuthResponseBody = AuthResponseBody {
id_token :: String
} deriving (Generic, Show)


data AccountResponseBody = AccountResponseBody {
activated :: Bool,
authorities :: [String],
email :: String,
firstName :: String,
langKey :: String,
lastName :: String,
login :: String
} deriving (Generic, Show)

instance ToJSON Auth
instance FromJSON AuthResponseBody
instance FromJSON AccountResponseBody

baseUrlServer = "http://localhost:8080/"
authenticateResource = baseUrlServer ++ "api/authenticate"
accountResource = baseUrlServer ++ "api/account"

auth login password =
let handler e@(StatusCodeException{}) = return Nothing
Expand All @@ -34,3 +48,13 @@ auth login password =
(body :: R.Response AuthResponseBody) <- R.asJSON r
return (body ^? R.responseBody)
in operations `E.catch` handler


whoami :: String -> IO (Maybe AccountResponseBody)
whoami token =
let handler e@(StatusCodeException{}) = return Nothing
options = R.defaults & R.header "Authorization" .~ [pack ("Bearer " ++ token)]
operations = do r <- R.getWith options accountResource
(body :: R.Response AccountResponseBody) <- R.asJSON r
return (body ^? R.responseBody)
in operations `E.catch` handler

0 comments on commit 2a754f3

Please sign in to comment.