Esempio n. 1
0
 def fromSSO(cls, ssoData):
     """
     Create a local user from openid sso
     """
     d = enum(**ssoData)
     self = cls(
             email=d.email,
             givenName=d.given_name,
             familyName=d.family_name,
             roles=[Roles.user],
             )
     self.save()
     return self
Esempio n. 2
0
 def fromSSO(cls, ssoData):
     """
     Create a local user from openid sso
     """
     d = enum(**ssoData)
     self = cls(
             email=d.email,
             givenName=d.given_name,
             familyName=d.family_name,
             roles=[Roles.user],
             )
     self.save()
     return self
Esempio n. 3
0
from klein import Klein

from noms import urlify, user, secret, CONFIG
from noms.recipe import Recipe
from noms.rendering import HumanReadable, RenderableQuerySet


TOKEN_URL = "https://{domain}/oauth/token".format(domain='nomsbook.auth0.com')
USER_URL = "https://{domain}/userinfo?access_token=".format(domain='nomsbook.auth0.com')
OAUTH_GRANT_TYPE = 'authorization_code'
RECIPE_SCHEMA = 'http://schema.org/Recipe'


ResponseMsg = enum(
        not_logged_in='User was not logged in.', 
        no_recipe='There are no recipes on this page.', 
        blank=''
        )


class Server(object):
    """
    The web server for html and miscell.
    """
    app = Klein()

    @app.route("/static/", branch=True)
    def static(self, request):
        return static.File("./static")

    @app.route("/")
Esempio n. 4
0
"""
Noms Python library - web application
"""
import re

from codado import fromdir, enum

from pymongo.uri_parser import parse_uri


fromNoms = fromdir(__file__, "..")


DBHost = enum(noms={"host": "mongodb://localhost/noms"}, nomsTest={"host": "mongomock://localhost/noms-test"})

# mongomock is broken, we need to maintain our own connection aliases
# See https://github.com/vmalloc/mongomock/issues/233 - we must parse
# host ourselves and pass in db=, for the benefit of mongomock.
DBAlias = enum.fromkeys(DBHost.keys())


def _parseHosts():
    """
    Build a dict of all of the connections defined by DBHost

    Doesn't register a default connection yet.
    """
    for k, v in DBHost.items():
        parts = parse_uri(v["host"].replace("mongomock", "mongodb"))  # hack for a parse_uri restriction
        DBHost[k]["db"] = parts["database"]
Esempio n. 5
0
    meta = {'abstract': True}

    def render(self, request):
        """
        => JSON-encoded representation of this object's safe properties
        """
        return json.dumps(self.safe(), cls=ResourceEncoder, sort_keys=True).encode('utf-8')

    def safe(self):
        """
        => dict of document's fields, safe for presentation to the browser
        """
        raise NotImplementedError("implement safe in a subclass")


ResponseStatus = enum(ok='ok', error='error')


@attr.s
class ResponseData(object):
    """
    Generic container for an API response
    """
    implements(resource.IResource)

    status = attr.ib()
    message = attr.ib(default='')

    def render(self, request):
        """
        => JSON-encoded representation of this object's safe properties
Esempio n. 6
0
from noms import urlify, secret, CONFIG
from noms.user import User, USER, Roles
from noms.recipe import Recipe
from noms import rendering
from noms.interface import ICurrentUser
from noms.rendering import ResponseStatus as RS, OK, ERROR

TOKEN_URL = "https://{domain}/oauth/token".format(domain='nomsbook.auth0.com')
USER_URL = "https://{domain}/userinfo?access_token=".format(
    domain='nomsbook.auth0.com')
OAUTH_GRANT_TYPE = 'authorization_code'
RECIPE_SCHEMA = 'http://schema.org/Recipe'

ResponseMsg = enum(
    notLoggedIn='User was not logged in.',
    noRecipe='There are no recipes on this page.',
    renameRecipe='You already have a recipe with the same name. Rename?',
)


def roles(allowed, forbidAction=Forbidden):
    """
    Request must belong to a user with the needed roles, or => 403
    """
    def wrapper(fn):
        @wraps(fn)
        def roleCheck(self, request, *a, **kw):
            u = ICurrentUser(request)
            for role in allowed:
                if role in u.roles:
                    return fn(self, request, *a, **kw)
Esempio n. 7
0
import re
import os

from codado import fromdir, enum

from pymongo.uri_parser import parse_uri


fromNoms = fromdir(__file__, '..')


NOMS_DB_HOST = os.environ.get('NOMS_DB_HOST', 'localhost')


DBHost = enum(
        noms={'host': 'mongodb://%s/noms' % NOMS_DB_HOST},
        nomsTest={'host': 'mongomock://localhost/noms-test'},
        )

# mongomock is broken, we need to maintain our own connection aliases
# See https://github.com/vmalloc/mongomock/issues/233 - we must parse
# host ourselves and pass in db=, for the benefit of mongomock.
DBAlias = enum.fromkeys(DBHost.keys())


def _parseHosts():
    """
    Build a dict of all of the connections defined by DBHost

    Doesn't register a default connection yet.
    """
    for k, v in DBHost.items():
Esempio n. 8
0
        Create a JSON token for this user
        """
        _, sec = secret.get('localapi')
        s = Serializer(sec, expires_in=secret.SECRET_EXPIRATION)
        return s.dumps({'email': self.email})


registerAdapter(User.fromRequest, Request, ICurrentUser)


_USERS = enum(
    anonymous=User(
        email='*****@*****.**',
        roles=[Roles.anonymous],
        givenName='Anonymous',
        ),
    localapi=User(
        email='*****@*****.**',
        roles=[Roles.localapi],
        givenName='Local API',
        ),
    )


def USER():
    """
    Ensure that the special users exist in the database
    
    => enum of those users
    """
    for k, _U in _USERS.items():
        if getattr(_U, 'id', None):