Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- service auth
- use {
- "std" 1.0
- "std/time" 1.0
- }
- User = entity {
- name String
- password String
- }
- Session = entity {
- user User
- creation Time
- }
- RUser = resolver { u User } {
- name = String => u.name
- sessions = Set<RSession> => entities<Session>(
- predicate: (s) => s.user == u,
- sortBy: Session.creation,
- )
- }
- RSession = resolver { s Session } {
- user = RUser => RUser{u: s.user}
- creation = Time => s.creation
- }
- RRoot = root {
- users Set<RUser> = authUser<Set<RUser>>(() => map(
- entities<User>(sortBy: User.name)),
- (u User) -> RUser => RUser{u: u},
- )
- }
- ErrWrongCredentials = Nil
- TrnLogin = Transaction<RSession>
- login = transaction(
- name String,
- password String,
- ) -> (
- ErrWrongCredentials or
- TrnLogin
- ) => {
- user = std::entity<User>(
- predicate: (u) => u.name == name && u.password == password,
- )
- newSession = Session{
- user: user as User,
- creation: time::now(),
- }
- & = user as u {
- User then TrnLogin {
- mutations: std::new(newSession),
- data: RSession{s: newSession},
- }
- else ErrWrongCredentials
- }
- }
- TrnLogout = std::Transaction<std::Nil>
- SessionId = std::ID<Session>
- logout = transaction(sessionId SessionId) -> TrnLogout => TrnLogout{
- mutations: std::delete(sessionId),
- }
- # Is returned when the client is unauthorized
- ErrUnauth = Nil
- # Equals a t:Session if there is one
- auth = () -> ?Session => {
- sid = SessionId(std::header("SID") as String or "")
- s = std::entity<Session>(
- predicate: (s Session) -> Bool => id(s) == sid as SessionId,
- )
- & = match {
- sid == SessionId && s == Session then s as Session
- }
- }
- # Equals t:D returned by p:data if the current client is a logged in user
- authUser = <D>(data ()->D) -> (D or ErrUnauth) => {
- & = auth() as {
- Session then data()
- else ErrUnauth
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement