Federated forge server
Clone
HTTPS:
git clone https://vervis.peers.community/repos/rjQ3E
SSH:
git clone USERNAME@vervis.peers.community:rjQ3E
Branches
Tags
Unverified.hs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | {- This file is part of Vervis.
-
- Written in 2018 by fr33domlover <fr33domlover@riseup.net>.
-
- ♡ Copying is an act of love. Please copy, reuse and share.
-
- This module is under MIT license because it's adapted from code taken from
- the yesod-auth library, which is:
-
- Copyright (c) 2012-2018 Michael Snoyman, http://www.yesodweb.com/
-
- Permission is hereby granted, free of charge, to any person obtaining
- a copy of this software and associated documentation files (the
- "Software"), to deal in the Software without restriction, including
- without limitation the rights to use, copy, modify, merge, publish,
- distribute, sublicense, and/or sell copies of the Software, and to
- permit persons to whom the Software is furnished to do so, subject to
- the following conditions:
-
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-}
-- | This module provides login for users with unverified accounts. It's simply
-- a separate login, implemented by copying what @yesod-auth@ does, except
-- using a different session key to store the user ID.
--
-- Implementing unverified logins this way allows control the requirement of
-- unverified login for authentication related features, or requirement of *at
-- least* unverified login, without asking the web app developer to do extra
-- work changing many other things in their code to adapt. With the method used
-- here, regular @yesod-auth@ login is by default verified, and if the only web
-- app features supporting unverified login are the authentication related
-- routes, the developer doesn't need to worry about unverified logins at all.
--
-- It's possibe that implementing unverified logins using @yesod-auth@ login
-- and a boolean session key to specify whether verified or not, would turn out
-- to be nice too, maybe even better. I haven't tried yet, and chose the
-- approach used below because it's less intrusive in the default case where
-- unverified login is used only for securing the verification features
-- themselves (asking to resend the verification link, and opening that link to
-- verify your account).
module Yesod.Auth.Unverified
( YesodAuthVerify (..)
, maybeUnverifiedAuthId
, maybeUnverifiedAuth
, maybeAuthIdAllowUnverified
, maybeAuthAllowUnverified
, maybeVerifiedAuthId
, maybeVerifiedAuth
, requireUnverifiedAuthId
, requireUnverifiedAuth
, requireAuthIdAllowUnverified
, requireAuthAllowUnverified
, requireVerifiedAuthId
, requireVerifiedAuth
)
where
import Control.Applicative ((<|>))
import Control.Monad (when)
import Control.Monad.Trans.Maybe
import Data.Typeable (Typeable)
import Database.Persist.Class
import Database.Persist.Types (Entity)
import Web.PathPieces (PathPiece)
import Yesod.Auth
import Yesod.Core (Yesod (authRoute), MonadHandler (HandlerSite))
import Yesod.Core.Handler
import Yesod.Core.Json (acceptsJson)
import Yesod.Persist.Core (YesodPersist (YesodPersistBackend))
import Yesod.Auth.Unverified.Internal
import Yesod.SessionEntity
newtype CachedUnverifiedLogin a = CachedUnverifiedLogin
{ unCachedUnverifiedLogin :: Maybe a
}
deriving Typeable
maybeUnverifiedAuthId
:: ( MonadHandler m
, HandlerSite m ~ master
, YesodPersist master
, YesodPersistBackend master ~ backend
, PersistStoreRead backend
, YesodAuth master
, AuthId master ~ Key record
, PersistRecordBackend record backend
, PathPiece (Key record)
, Typeable record
)
=> m (Maybe (Key record))
maybeUnverifiedAuthId =
maybeKey unverifiedLoginKey CachedUnverifiedLogin unCachedUnverifiedLogin
maybeUnverifiedAuth
:: ( MonadHandler m
, HandlerSite m ~ master
, YesodPersist master
, YesodPersistBackend master ~ backend
, PersistStoreRead backend
, YesodAuth master
, AuthId master ~ Key record
, PersistRecordBackend record backend
, PathPiece (Key record)
, Typeable record
)
=> m (Maybe (Entity record))
maybeUnverifiedAuth =
maybeEntity unverifiedLoginKey CachedUnverifiedLogin unCachedUnverifiedLogin
maybeAuthIdAllowUnverified
:: ( MonadHandler m
, HandlerSite m ~ master
, YesodPersist master
, YesodPersistBackend master ~ backend
, PersistStoreRead backend
, YesodAuth master
, AuthId master ~ Key record
, PersistRecordBackend record backend
, Typeable record
)
=> m (Maybe (Key record, Bool))
maybeAuthIdAllowUnverified = runMaybeT $
(, True) <$> MaybeT maybeVerifiedAuthId
<|> (, False) <$> MaybeT maybeUnverifiedAuthId
maybeAuthAllowUnverified
:: ( MonadHandler m
, HandlerSite m ~ master
, YesodPersist master
, YesodPersistBackend master ~ backend
, PersistStoreRead backend
, YesodAuthPersist master
, AuthId master ~ Key record
, AuthEntity master ~ record
, PersistRecordBackend record backend
, Typeable record
)
=> m (Maybe (Entity record, Bool))
maybeAuthAllowUnverified = runMaybeT $
(, True) <$> MaybeT maybeVerifiedAuth
<|> (, False) <$> MaybeT maybeUnverifiedAuth
maybeVerifiedAuthId
:: ( MonadHandler m
, HandlerSite m ~ master
, YesodAuth master
)
=> m (Maybe (AuthId master))
maybeVerifiedAuthId = maybeAuthId
maybeVerifiedAuth
:: ( MonadHandler m
, HandlerSite m ~ master
, YesodAuthPersist master
, AuthId master ~ Key record
, AuthEntity master ~ record
, PersistEntity record
, Typeable record
)
=> m (Maybe (Entity record))
maybeVerifiedAuth = maybeAuth
handleAuthLack :: (YesodAuth (HandlerSite m), MonadHandler m) => m a
handleAuthLack = do
aj <- acceptsJson
if aj
then notAuthenticated
else do
y <- getYesod
when (redirectToCurrent y) setUltDestCurrent
case authRoute y of
Just z -> redirect z
Nothing -> permissionDenied "Please configure authRoute"
handleUnverified
:: (MonadHandler m, YesodAuthVerify (HandlerSite m))
=> (a, Bool) -> m a
handleUnverified (v, True) = return v
handleUnverified (_v, False) = do
aj <- acceptsJson
if aj
then permissionDenied "Please verify your account first"
else do
setMessage "Please verify your account first"
y <- getYesod
when (redirectToCurrent y) setUltDestCurrent
redirect $ verificationRoute y
handleVerified
:: (MonadHandler m, YesodAuth (HandlerSite m))
=> (a, Bool) -> m a
handleVerified (v, False) = return v
handleVerified (_v, True) = do
aj <- acceptsJson
if aj
then permissionDenied "This route is only for unverified users"
else do
setMessage "This page is only for unverified users"
y <- getYesod
redirectUltDest $ loginDest y
-- | Similar to 'maybeAuthId', but redirects to a login page if user is not
-- authenticated or responds with error 401 if this is an API client (expecting JSON).
--
-- @since 1.1.0
requireUnverifiedAuthId
:: ( MonadHandler m
, HandlerSite m ~ master
, YesodPersist master
, YesodPersistBackend master ~ backend
, PersistStoreRead backend
, YesodAuth master
, AuthId master ~ Key record
, PersistRecordBackend record backend
, Typeable record
)
=> m (Key record)
requireUnverifiedAuthId =
maybeAuthIdAllowUnverified >>= maybe handleAuthLack handleVerified
-- | Similar to 'maybeAuth', but redirects to a login page if user is not
-- authenticated or responds with error 401 if this is an API client (expecting JSON).
--
-- @since 1.1.0
requireUnverifiedAuth
:: ( MonadHandler m
, HandlerSite m ~ master
, YesodPersist master
, YesodPersistBackend master ~ backend
, PersistStoreRead backend
, YesodAuthPersist master
, AuthId master ~ Key record
, AuthEntity master ~ record
, PersistRecordBackend record backend
, Typeable record
)
=> m (Entity record)
requireUnverifiedAuth =
maybeAuthAllowUnverified >>= maybe handleAuthLack handleVerified
requireAuthIdAllowUnverified
:: ( MonadHandler m
, HandlerSite m ~ master
, YesodPersist master
, YesodPersistBackend master ~ backend
, PersistStoreRead backend
, YesodAuth master
, AuthId master ~ Key record
, PersistRecordBackend record backend
, Typeable record
)
=> m (Key record, Bool)
requireAuthIdAllowUnverified =
maybeAuthIdAllowUnverified >>= maybe handleAuthLack return
requireAuthAllowUnverified
:: ( MonadHandler m
, HandlerSite m ~ master
, YesodPersist master
, YesodPersistBackend master ~ backend
, PersistStoreRead backend
, YesodAuthPersist master
, AuthId master ~ Key record
, AuthEntity master ~ record
, PersistRecordBackend record backend
, Typeable record
)
=> m (Entity record, Bool)
requireAuthAllowUnverified =
maybeAuthAllowUnverified >>= maybe handleAuthLack return
requireVerifiedAuthId
:: ( MonadHandler m
, HandlerSite m ~ master
, YesodPersist master
, YesodPersistBackend master ~ backend
, PersistStoreRead backend
, YesodAuthVerify master
, AuthId master ~ Key record
, PersistRecordBackend record backend
, Typeable record
)
=> m (Key record)
requireVerifiedAuthId =
maybeAuthIdAllowUnverified >>= maybe handleAuthLack handleUnverified
requireVerifiedAuth
:: ( MonadHandler m
, HandlerSite m ~ master
, YesodPersist master
, YesodPersistBackend master ~ backend
, PersistStoreRead backend
, YesodAuthPersist master
, YesodAuthVerify master
, AuthId master ~ Key record
, AuthEntity master ~ record
, PersistRecordBackend record backend
, Typeable record
)
=> m (Entity record)
requireVerifiedAuth =
maybeAuthAllowUnverified >>= maybe handleAuthLack handleUnverified
|