This repository was archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathBasicRBAC.scala
49 lines (42 loc) · 1.54 KB
/
BasicRBAC.scala
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
package tsec.authorization
import cats.MonadError
import cats.data.OptionT
import cats.syntax.functor._
import tsec.authentication
import scala.reflect.ClassTag
sealed abstract case class BasicRBAC[F[_], R, U, Auth](authorized: AuthGroup[R])(
implicit role: AuthorizationInfo[F, R, U],
authEnum: SimpleAuthEnum[R, String],
F: MonadError[F, Throwable]
) extends Authorization[F, U, Auth] {
def isAuthorized(
toAuth: authentication.SecuredRequest[F, U, Auth]
): OptionT[F, authentication.SecuredRequest[F, U, Auth]] =
OptionT {
role.fetchInfo(toAuth.identity).map { extractedRole =>
if (authEnum.contains(extractedRole) && authorized.contains(extractedRole))
Some(toAuth)
else
None
}
}
}
object BasicRBAC {
def apply[F[_], R: ClassTag, U, Auth](roles: R*)(
implicit authEnum: SimpleAuthEnum[R, String],
role: AuthorizationInfo[F, R, U],
F: MonadError[F, Throwable]
): BasicRBAC[F, R, U, Auth] =
fromGroup[F, R, U, Auth](AuthGroup(roles: _*))
def fromGroup[F[_], R: ClassTag, U, Auth](valueSet: AuthGroup[R])(
implicit role: AuthorizationInfo[F, R, U],
authEnum: SimpleAuthEnum[R, String],
F: MonadError[F, Throwable]
): BasicRBAC[F, R, U, Auth] = new BasicRBAC[F, R, U, Auth](valueSet) {}
def all[F[_], R: ClassTag, U, Auth](
implicit authEnum: SimpleAuthEnum[R, String],
role: AuthorizationInfo[F, R, U],
F: MonadError[F, Throwable]
): BasicRBAC[F, R, U, Auth] =
new BasicRBAC[F, R, U, Auth](authEnum.viewAll) {}
}