Пример #1
0
    def createToken(self, args):
        self._start(args)
        # Do a check to see if any admins exist before proceeding.
        print('Create token: please authenticate...')
        
        while True:
            admin_username = raw_input('Enter admin username: '******'Enter admin password: '******'Please sign in using administrator credentials.')
            elif not User.users().get(username=admin_username).authenticate(admin_password):
                print("Username/password don't match.")
            else:
                break

        username = raw_input('Enter username to create a token for: ')
        user = User.users().get(username=username)
        if user:
            try:
                token = Token(user=user)
                token.save()
                print(str('Token %s created for %s' % (token.token, username)))
            except Exception as e:
                print("EXCEPTION **** %s" % e)
                print('A token already exists for %s. Doing nothing.' % username)
        else:
            print('A user with username %s does not exist.' % username)
Пример #2
0
 def get(self):
     try:
         username = self.request.headers.get('username')
         password = self.request.headers.get('password')
         user = User.users().get(username=username)
         if user.authenticate(password):
             token = Token.tokens().get_token_for_user(user)
             if not token:
                 token = Token(user=user)
                 token.save()
             data = {'token': token.token}
         else:
             data = {'error': 'invalid username/password'}
     except Exception as e:
         data = {'error': str(e)}
     self.set_header('Content-Type', 'application/json')
     self.write(json.dumps(data))