Ejemplo n.º 1
0
Archivo: oauth.py Proyecto: scality/eve
def renderLogin(self, request):
    code = request.args.get(b"code", [b""])[0]
    token = request.args.get(b"token", [b""])[0]
    if not token and not code:
        url = request.args.get(b"redirect", [None])[0]
        url = yield self.auth.getLoginURL(url)
        raise resource.Redirect(url)

    if not token:
        details = yield self.auth.verifyCode(code)
    else:
        details = yield self.auth.acceptToken(token)
    if self.auth.userInfoProvider is not None:
        infos = yield self.auth.userInfoProvider.getUserInfo(
            details['username'])
        details.update(infos)
    session = request.getSession()
    session.user_info = details
    session.updateSession(request)
    state = request.args.get(b"state", [b""])[0]
    if state:
        # pylint: disable=E0602
        for redirect in parse_qs(state).get('redirect', []):  # noqa: F821
            raise resource.Redirect(self.auth.homeUri + "#" + redirect)
    raise resource.Redirect(self.auth.homeUri)
Ejemplo n.º 2
0
 def renderAvatar(self, request):
     email = request.args.get(b"email", [b""])[0]
     size = request.args.get(b"size", [32])[0]
     try:
         size = int(size)
     except ValueError:
         size = 32
     username = request.args.get(b"username", [None])[0]
     cache_key = (email, username, size)
     if self.cache.get(cache_key):
         raise self.cache[cache_key]
     for method in self.avatarMethods:
         try:
             res = yield method.getUserAvatar(email, username, size,
                                              self.defaultAvatarFullUrl)
         except resource.Redirect as r:
             self.cache[cache_key] = r
             raise
         if res is not None:
             request.setHeader(b'content-type', res[0])
             request.setHeader(b'content-length',
                               unicode2bytes(str(len(res[1]))))
             request.write(res[1])
             return
     raise resource.Redirect(self.defaultAvatarUrl)
Ejemplo n.º 3
0
 def getUserAvatar(self, email, size, defaultAvatarUrl):
     # construct the url
     gravatar_url = "//www.gravatar.com/avatar/"
     gravatar_url += hashlib.md5(email.lower()).hexdigest() + "?"
     if self.default != "url":
         defaultAvatarUrl = self.default
     gravatar_url += urlencode({'s': str(size), 'd': defaultAvatarUrl})
     raise resource.Redirect(gravatar_url)
Ejemplo n.º 4
0
 def renderLogin(self, request):
     code = request.args.get("code", [""])[0]
     if not code:
         url = yield self.auth.getLoginURL()
         defer.returnValue(url)
     else:
         details = yield self.auth.verifyCode(code)
         request.getSession().user_info = details
         raise resource.Redirect(self.auth.homeUri)
Ejemplo n.º 5
0
 def renderLogin(self, request):
     code = request.args.get("code", [""])[0]
     if not code:
         url = request.args.get("redirect", [None])[0]
         url = yield self.auth.getLoginURL(url)
         raise resource.Redirect(url)
     else:
         details = yield self.auth.verifyCode(code)
         if self.auth.userInfoProvider is not None:
             infos = yield self.auth.userInfoProvider.getUserInfo(
                 details['username'])
             details.update(infos)
         request.getSession().user_info = details
         state = request.args.get("state", [""])[0]
         if state:
             for redirect in parse_qs(state).get('redirect', []):
                 raise resource.Redirect(self.auth.homeUri + "#" + redirect)
         raise resource.Redirect(self.auth.homeUri)
Ejemplo n.º 6
0
 def getUserAvatar(self, email, size, defaultAvatarUrl):
     # construct the url
     emailBytes = unicode2bytes(email.lower())
     emailHash = hashlib.md5(emailBytes)
     gravatar_url = "//www.gravatar.com/avatar/"
     gravatar_url += emailHash.hexdigest() + "?"
     if self.default != "url":
         defaultAvatarUrl = self.default
     url = {'d': defaultAvatarUrl, 's': str(size)}
     sorted_url = sorted(url.items(), key=lambda x: x[0])
     gravatar_url += urlencode(sorted_url)
     raise resource.Redirect(gravatar_url)
Ejemplo n.º 7
0
 def renderAvatar(self, request):
     email = request.args.get(b"email", [b""])[0]
     size = request.args.get(b"size", 32)
     if self.cache.get(email):
         r = self.cache[email]
     for method in self.avatarMethods:
         try:
             res = yield method.getUserAvatar(email, size, self.defaultAvatarFullUrl)
         except resource.Redirect as r:
             self.cache[email] = r
             raise
         if res is not None:
             request.setHeader(b'content-type', res[0])
             request.setHeader(b'content-length', len(res[1]))
             request.write(res[1])
             return
     raise resource.Redirect(self.defaultAvatarUrl)
Ejemplo n.º 8
0
    def getUserAvatar(self, email, username, size, defaultAvatarUrl):
        avatar = None
        if username:
            username = username.decode('utf-8')
        if email:
            email = email.decode('utf-8')

        if username:
            avatar = yield self._get_avatar_by_username(username)
        if not avatar and email:
            # Try searching a user with said mail
            avatar = yield self._search_avatar_by_user_email(email)
        if not avatar and email:
            # No luck, try to find a commit with this email
            avatar = yield self._search_avatar_by_commit(email)

        if not avatar:
            # No luck
            return None

        if size:
            avatar = self._add_size_to_url(avatar, size)

        raise resource.Redirect(avatar)
Ejemplo n.º 9
0
class AvatarResource(resource.Resource):
    # enable reconfigResource calls
    needsReconfig = True
    defaultAvatarUrl = "img/nobody.png"

    def reconfigResource(self, new_config):
        self.avatarMethods = new_config.www.get('avatar_methods', [])
        self.defaultAvatarFullUrl = urljoin(new_config.buildbotURL,
                                            self.defaultAvatarUrl)
        self.cache = {}
        # ensure the avatarMethods is a iterable
        if isinstance(self.avatarMethods, AvatarBase):
            self.avatarMethods = (self.avatarMethods, )

    def render_GET(self, request):
        return self.asyncRenderHelper(request, self.renderAvatar)

    @defer.inlineCallbacks
    def renderAvatar(self, request):
        email = request.args.get("email", [""])[0]
        size = request.args.get("size", 32)
        if self.cache.get(email):
            r = self.cache[email]
        for method in self.avatarMethods:
            try:
                res = yield method.getUserAvatar(email, size,
                                                 self.defaultAvatarFullUrl)
            except resource.Redirect, r:
                self.cache[email] = r
                raise
            if res is not None:
                request.setHeader('content-type', res[0])
                request.setHeader('content-length', len(res[1]))
                request.write(res[1])
                return
        raise resource.Redirect(self.defaultAvatarUrl)
Ejemplo n.º 10
0
def _redirect(master, request):
    url = request.args.get(b"redirect", [b"/"])[0]
    url = bytes2unicode(url)
    return resource.Redirect(master.config.buildbotURL + "#" + url)
Ejemplo n.º 11
0
 def getUserAvatar(self, email, username, size, defaultAvatarUrl):
     github_url = self.AVATAR_URL + username.decode("utf-8")
     raise resource.Redirect(github_url)
Ejemplo n.º 12
0
def _redirect(master, request):
    url = request.args.get("redirect", ["/"])[0]
    return resource.Redirect(master.config.buildbotURL + "#" + url)