def _setup_clients(self, scope='email'): self.setup_course() self.oauth_client = Client( name='Testing Client', client_id='normal', client_secret='normal', redirect_uris=['http://127.0.0.1:8000/authorized'], is_confidential=False, active=True, description='Sample App for testing OAuth', default_scopes=scope) db.session.add(self.oauth_client) db.session.commit() self.temp_grant = Grant(user_id=self.user1.id, client_id='normal', code='12345', scopes=['email'], expires=dt.datetime.utcnow() + dt.timedelta(seconds=100)) db.session.add(self.temp_grant) self.expired_token = Token(user_id=self.user1.id, client_id='normal', scopes=[scope], access_token='expired', expires=dt.datetime.utcnow() - dt.timedelta(seconds=1)) db.session.add(self.expired_token) self.valid_token = Token(user_id=self.user1.id, client_id='normal', scopes=[scope], access_token='soo_valid', expires=dt.datetime.utcnow() + dt.timedelta(seconds=3600)) db.session.add(self.valid_token) self.valid_token_bad_scope = Token(user_id=self.user1.id, client_id='normal', scopes=['invalid'], access_token='soo_valid12', expires=dt.datetime.utcnow() + dt.timedelta(seconds=3600)) db.session.add(self.valid_token_bad_scope) self.valid_token_all_scope = Token(user_id=self.user1.id, client_id='normal', scopes=['all'], access_token='soo_valid322', expires=dt.datetime.utcnow() + dt.timedelta(seconds=3600)) db.session.add(self.valid_token_all_scope) db.session.commit()
def create_autograder_token(user_id): autograder_client = Client.query.get('autograder') if not autograder_client: autograder_client = Client( name='Autograder', client_id='autograder', client_secret='autograder', redirect_uris=[], is_confidential=False, description='The Autopy autograder system', default_scopes=['all'], ) db.session.add(autograder_client) db.session.commit() token = Token( client=autograder_client, user_id=user_id, token_type='bearer', access_token=oauthlib.common.generate_token(), expires=datetime.datetime.utcnow() + datetime.timedelta(hours=2), scopes=['all'], ) db.session.add(token) db.session.commit() return token
def mutate( self, info, created, livemode, type, card, status, ): user = info.context.user if user.is_anonymous: return CreateStripeToken(ok=False, status="Must be logged in") else: new_token = Token( created=created, livemode=livemode, type=type, card=card, status=status, ) new_token.save() return CreateStripeToken(token=new_token, ok=True, status="ok")
def save_token(token, orequest, *args, **kwargs): toks = Token.query.filter_by(client_id=orequest.client.client_id, user_id=orequest.user.id).all() # make sure that every client has only one token connected to a user for t in toks: db.session.delete(t) expires_in = token.get('expires_in') expires = dt.datetime.utcnow() + dt.timedelta(seconds=expires_in) tok = Token( access_token=token['access_token'], refresh_token=token['refresh_token'], token_type=token['token_type'], scopes=token['scope'].split(), expires=expires, client_id=orequest.client.client_id, user_id=orequest.user.id, ) db.session.add(tok) db.session.commit() return tok
def send_batch(assignment, backup_ids): if not assignment.autograding_key: raise ValueError('Assignment has no autograder key') # Create an access token for this run autograder_client = Client.query.get('autograder') if not autograder_client: autograder_client = Client( name='Autograder', client_id='autograder', client_secret='autograder', redirect_uris=[], is_confidential=False, description='The Autopy autograder system', default_scopes=['all'], ) db.session.add(autograder_client) db.session.commit() token = Token( client=autograder_client, user=current_user, token_type='bearer', access_token=oauthlib.common.generate_token(), expires=datetime.datetime.utcnow() + datetime.timedelta(hours=2), scopes=['all'], ) db.session.add(token) db.session.commit() return send_autograder('/api/ok/v3/grade/batch', { 'subm_ids': [utils.encode_id(bid) for bid in backup_ids], 'assignment': assignment.autograding_key, 'access_token': token.access_token, 'priority': 'default', 'backup_url': url_for('api.backup', _external=True), 'ok-server-version': 'v3', })