module Fedora.Krb (
  krbTicket,
  maybeFasIdFromKrb,
  fasIdFromKrb
  )
where

import Control.Monad
import qualified Data.List as L
import Data.Maybe
import SimpleCmd

krbTicket :: IO ()
krbTicket :: IO ()
krbTicket = do
  [String]
entry <- IO [String]
klistEntryFedora
  if [String] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
entry
    then String -> IO ()
forall a. String -> a
error' String
"No krb5 ticket found for FEDORAPROJECT.ORG"
    else
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([String] -> String
forall a. HasCallStack => [a] -> a
last [String]
entry String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"(Expired)") (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
    String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ [String] -> String
unwords [String]
entry
    Maybe String -> IO ()
fkinit (Maybe String -> IO ()) -> Maybe String -> IO ()
forall a b. (a -> b) -> a -> b
$ [String] -> Maybe String
extractFasId [String]
entry
    String -> IO ()
putStrLn String
""
  where
    fkinit :: Maybe String -> IO ()
fkinit Maybe String
muser = do
      let opts :: [String]
opts = [String] -> (String -> [String]) -> Maybe String -> [String]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\String
user -> [String
"-u", String
user]) Maybe String
muser
      -- FIXME test for fkinit
      Bool
ok <- String -> [String] -> IO Bool
cmdBool String
"fkinit" [String]
opts
      Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
ok (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Maybe String -> IO ()
fkinit Maybe String
muser

extractFasId :: [String] -> Maybe String
extractFasId :: [String] -> Maybe String
extractFasId =
  (String -> String) -> Maybe String -> Maybe String
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String -> String -> String
removeSuffix String
"@FEDORAPROJECT.ORG") (Maybe String -> Maybe String)
-> ([String] -> Maybe String) -> [String] -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Bool) -> [String] -> Maybe String
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
L.find (String
"@FEDORAPROJECT.ORG" String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`L.isSuffixOf`)

maybeFasIdFromKrb :: IO (Maybe String)
maybeFasIdFromKrb :: IO (Maybe String)
maybeFasIdFromKrb = [String] -> Maybe String
extractFasId ([String] -> Maybe String) -> IO [String] -> IO (Maybe String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO [String]
klistEntryFedora

fasIdFromKrb :: IO String
fasIdFromKrb :: IO String
fasIdFromKrb = do
  Maybe String
mfasid <- IO (Maybe String)
maybeFasIdFromKrb
  case Maybe String
mfasid of
    Maybe String
Nothing -> String -> IO String
forall a. String -> a
error' String
"Could not determine FAS id from klist"
    Just String
fasid -> String -> IO String
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return String
fasid

-- gets first FEDORAPROJECT.ORG entry
klistEntryFedora :: IO [String]
klistEntryFedora :: IO [String]
klistEntryFedora = do
  -- FIXME test for klist
  Maybe String
mres <- String -> [String] -> IO (Maybe String)
cmdMaybe String
"klist" [String
"-l"]
  [String] -> IO [String]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([String] -> IO [String]) -> [String] -> IO [String]
forall a b. (a -> b) -> a -> b
$
    [String] -> (String -> [String]) -> Maybe String -> [String]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe []
    (String -> [String]
words (String -> [String]) -> (String -> String) -> String -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
"" (Maybe String -> String)
-> (String -> Maybe String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Bool) -> [String] -> Maybe String
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
L.find (String
"@FEDORAPROJECT.ORG" String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`L.isInfixOf`) ([String] -> Maybe String)
-> (String -> [String]) -> String -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
lines)
    Maybe String
mres