def login(self, request): data = verify(request.POST["assertion"], self.audience) if data and "email" in data: email = data["email"] user, created = User.create_or_get(email=email) response = ResponseRedirect(self.login_success_url) encoded = jwt.encode({"email": user.email}, self.jwt_key, algorithm=self.jwt_algorithm) response.set_cookie("jwt", encoded, secure=True) return response return ResponseRedirect(self.login_failure_url)
def callback(request): code = request.GET.get('code') if not code: error = request.GET.get('error') error_description = request.GET.get('error_description', 'Unknown Error, please try again') return JinjaResponse( request, template_names=['error.html'], context={'reason': '{} ({})'.format(error_description, error)}) access_token = github.retrieve_access_token(code) if not access_token: return ResponseRedirect('https://sotu.cocoapods.org/') user = github.retrieve_account(access_token) email = github.retrieve_email(access_token) username = user['login'] name = user.get('name', username) if name is None or len(name) == 0: name = username avatar = user.get('avatar_url', None) try: entrant = Entrant.select().where( Entrant.github_username == username).get() except Entrant.DoesNotExist: entrant = Entrant.create(github_username=username, name=name, email=email) try: invitation = entrant.invitation_set.get() except Invitation.DoesNotExist: invitation = None if invitation: if invitation.state == Invitation.ACCEPTED_STATE: return ResponseRedirect(invitation.accept_url) elif invitation.state == Invitation.REJECTED_STATE: return ResponseRedirect(invitation.reject_url) elif invitation.state == Invitation.INVITED_STATE: return ResponseRedirect(invitation.invited_url) return EntrantView.as_view(entrant=entrant, avatar=avatar)(request)
def get(self, *args, **kwargs): try: self.invitation = Invitation.select().where( Invitation.code == kwargs['code']).get() except Invitation.DoesNotExist: raise Http404 if self.invitation.state not in self.expected_states: raise Http404 if self.invitation.state == Invitation.REMOVED_STATE: return ResponseRedirect('https://sotu.cocoapods.org/removed') if self.enforce_attendee_limit and self.has_reached_limit and self.invitation.state == Invitation.INVITED_STATE: return ResponseRedirect('https://sotu.cocoapods.org/cap') self.perform(self.invitation) return super(InvitationView, self).get(*args, **kwargs)
def logout(self, request): response = ResponseRedirect(self.logout_redirect_url) response.delete_cookie("jwt") return response