18import Data.ByteString as BS
19import Data.ByteString.Base64.URL as B64
22import Data.Text.Encoding as T
24import Test.QuickCheck.Arbitrary
29import Crypto.Random as R
30import Database.PostgreSQL.Simple.FromField
31import Database.PostgreSQL.Simple.ToField
34-- | 16 bytes lenght random value parametrized by label.
35newtype Token (a :: k) = Token ByteString
36 deriving (Ord, Eq, Show, NFData)
38instance Arbitrary (Token a) where
39 arbitrary = fmap (Token . BS.pack) $ sequence $ L.replicate 16 arbitrary
41instance FromJSON (Token a) where
44 either fail return $ textToToken t
46instance ToJSON (Token a) where
47 toJSON = toJSON . tokenToText
50instance forall a. (Typeable (Token a), Typeable a) => FromField (Token a) where
54 Left e -> returnError ConversionFailed fld e
57instance ToField (Token a) where
58 toField = toField . tokenToText
60generateToken :: (MonadRandom m) => m (Token a)
61generateToken = Token <$> R.getRandomBytes 16
65instance FromHttpApiData (Token a) where
66 parseQueryParam = first T.pack . textToToken
68instance ToHttpApiData (Token a) where
69 toQueryParam = tokenToText
71tokenToAscii :: Token a -> ByteString
72tokenToAscii (Token bs) = B64.encode bs
74tokenToText :: Token a -> Text
75tokenToText = T.decodeUtf8 . tokenToAscii
77asciiToToken :: ByteString -> Either String (Token a)
78asciiToToken bs = Token <$> B64.decode bs
80textToToken :: Text -> Either String (Token a)
81textToToken = asciiToToken . T.encodeUtf8
83tokenWithContents :: Text -> Token a
84tokenWithContents = Token . T.encodeUtf8
86unsafeCoerceToken :: Token a -> Token b
87unsafeCoerceToken (Token a) = Token a
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list