source: main/waeup.cas/trunk/waeup/cas/db.py @ 10406

Last change on this file since 10406 was 10406, checked in by uli, 11 years ago

Service tickets now store SSO flag in db.

File size: 3.7 KB
Line 
1# components to store tickets in a database
2# we use sqlite3
3import time
4from sqlalchemy import (
5    create_engine, Column, Integer, Float, String, Boolean, MetaData)
6from sqlalchemy.ext.declarative import declarative_base
7from sqlalchemy.orm import sessionmaker, scoped_session
8
9
10Base = declarative_base()
11
12
13class ServiceTicket(Base):
14    __tablename__ = 'service_tickets'
15
16    id = Column(Integer, primary_key=True)
17    ticket = Column(String(255), nullable=False)
18    ts = Column(Float, nullable=False)
19    service = Column(String(2048), nullable=True)
20    user = Column(String(512), nullable=False)
21    sso = Column(Boolean, nullable=False, default=True)
22
23    def __init__(self, ticket, user, service=None, sso=True, timestamp=None):
24        if timestamp is None:
25            timestamp = time.time()
26        self.ticket = ticket
27        self.ts = timestamp
28        self.service = service
29        self.user = user
30        self.sso = sso
31
32    def __repr__(self):
33        return "ServiceTicket('%s', '%s', '%s', %s, %s)" % (
34            self.ticket, self.user, self.service, self.sso, self.ts)
35
36
37class LoginTicket(Base):
38    __tablename__ = 'login_tickets'
39
40    ticket = Column(String(255), primary_key=True)
41    ts = Column(Float, nullable=False)
42
43    def __init__(self, ticket, timestamp=None):
44        if timestamp is None:
45            timestamp = time.time()
46        self.ticket = ticket
47        self.ts = timestamp
48
49    def __repr__(self):
50        return "LoginTicket('%s', %s)" % (self.ticket, self.ts)
51
52
53class TicketGrantingCookie(Base):
54    __tablename__ = 'ticket_granting_cookies'
55
56    value = Column(String(255), primary_key=True)
57    ts = Column(Float, nullable=False)
58
59    def __init__(self, value, timestamp=None):
60        if timestamp is None:
61            timestamp = time.time()
62        self.value = value
63        self.ts = timestamp
64
65    def __repr__(self):
66        return "TicketGrantingCookie('%s', %s)" % (self.value, self.ts)
67
68
69Session = scoped_session(sessionmaker())
70
71
72class DB(object):
73    """Abstract database to make data persistent.
74
75    Creates a database identified by `connection_string` if it does
76    not exist and initializes sessions.
77    """
78    @property
79    def session(self):
80        return Session
81
82    def __init__(self, connection_string):
83        self.engine = create_engine(connection_string)
84        self.metadata = MetaData()
85        Base.metadata.create_all(self.engine)
86        Session.configure(bind=self.engine)
87
88    def add(self, item):
89        """Insert `item` into database.
90
91        `item` must be an instance of the database content types
92        defined in this module, normally some `Ticket` instance.
93        """
94        Session.add(item)
95        Session.commit()
96
97    def delete(self, item):
98        """Delete `item` from database.
99
100        `item must be an instance of the database content types
101        defined in this module, normally some `Ticket` instance.
102        """
103        Session.delete(item)
104        Session.commit()
105
106    def query(self, *args, **kw):
107        return Session.query(*args, **kw)
108
109
110class DBSessionContext(object):
111    """A context manager providing database sessions.
112
113    Creates a new (threadlocal) database session when entering and
114    tears down the session after leaving the context.
115
116    Tearing down includes committing transactions and closing the
117    connection.
118
119    Meant to be used as a wrapper for web request handlers, such, that
120    a session is created when a request comes in and released when
121    the response is ready to be delivered.
122
123    This context manager does *not* catch any exceptions.
124    """
125    def __enter__(self):
126        return Session()
127
128    def __exit__(self, *args, **kw):
129        if args or kw:
130            Session.rollback()
131        Session.remove()
132        return False
Note: See TracBrowser for help on using the repository browser.