コード例 #1
0
ファイル: modulemanager.py プロジェクト: qbx2/opencampus
def check_gateway(access_token, campus_id):
    if not access_token or access_token.access_obj_type != 'gateway':
        return None
    try:
        return CampusGateway.objects(id=access_token.access_obj_id,
                                     campus_id=campus_id).get()
    except CampusGateway.DoesNotExist:
        return None
コード例 #2
0
ファイル: handler.py プロジェクト: qbx2/opencampus
def campus_gateways(campus_id):
    return render_template(
        'developer/console/campus/gateways.html',
        campus=Campus.objects(id=campus_id).get(),
        gateways=CampusGateway.objects(campus_id=campus_id),
        menu='gateways',
        gateway_method_ids=GATEWAY_METHOD_IDS,
        module_menus=modulemanager.get_manager_menus()
    )
コード例 #3
0
ファイル: handler.py プロジェクト: qbx2/opencampus
def campus_gateway_method(campus_id, gateway_id):
    gateway = CampusGateway.objects(id=gateway_id, campus_id=campus_id).get()

    if not gateway:
        return abort(404)

    if request.form.get('method') == 'reset_secret_key':
        gateway.reset_secret_key()
        gateway.save()
    elif request.form.get('method') == 'delete':
        gateway.delete()

    return redirect(url_for('console.campus_gateways', campus_id=campus_id))
コード例 #4
0
ファイル: gateway.py プロジェクト: qbx2/opencampus
    def _get_gateway_and_url(self, action):
        action = self.apis.get(action)
        if not action:
            return None, None

        gateway_id = action.get('gateway_id')
        gateway = self.gateway.get(gateway_id)
        if not gateway:
            from opencampus.common.models import CampusGateway
            try:
                gateway_obj = CampusGateway.objects(id=gateway_id, campus_id=self.campus.id).get()
            except CampusGateway.DoesNotExist:
                return None, None
            gateway = GatewayItem(gateway_obj)
            self.gateway[gateway_id] = gateway

        return gateway, action.get('url')
コード例 #5
0
ファイル: handler.py プロジェクト: qbx2/opencampus
def campus_save_gateway_apis(campus_id):
    campus = Campus.objects(id=campus_id).get()
    apis = json.loads(campus.gateway_apis) if campus.gateway_apis else {}

    for method in GATEWAY_METHOD_IDS:
        gateway_id = request.form.get(method + '_gateway')
        url = request.form.get(method + '_url')
        try:
            gateway = CampusGateway.objects(id=gateway_id, campus_id=campus_id).get()
        except CampusGateway.DoesNotExist:
            return abort(400)

        apis[method] = {
            'gateway_id': gateway_id,
            'url': url
        }

    campus.gateway_apis = json.dumps(apis)
    campus.save()
    return redirect(url_for('console.campus_gateways', campus_id=campus_id))
コード例 #6
0
ファイル: blueprint.py プロジェクト: qbx2/opencampus
def oauth2_token():
    grant_type = request.form.get('grant_type')

    if grant_type == 'authorization_code':
        """
        일반 사용자가 앱에서 토큰을 획득할 때 사용
        """
        client_id = request.form.get('client_id')
        client_secret = request.form.get('client_secret')
        redirect_uri = request.form.get('redirect_uri')     # TODO
        code = request.form.get('code')

        try:
            code = OAuth2AuthorizationCode.objects(code=code, client_id=client_id).get()
        except OAuth2AuthorizationCode.DoesNotExist:
            return jsonify({'error': 'invalid_request'}), 400

        try:
            client = ApplicationOAuth2Client.objects(id=code.client_id).get()
        except ApplicationOAuth2Client.DoesNotExist:
            return jsonify({'error': 'unauthorized_client'}), 400

        if client_secret != client.secret_key:
            return jsonify({'error': 'unauthorized_client'}), 400

        token = OAuth2AccessToken.create_token('account', code.account_id, scope=code.scope)
        token.client_id = client.id
        token.save()
        code.delete()

        expires_in = token.expires_at - datetime.utcnow()
        expires_in = expires_in.days * 86400 + expires_in.seconds
        return jsonify({
            'access_token': token.access_token,
            'expires_in': expires_in,
            'token_type': 'Bearer',
            'refresh_token': token.refresh_token
        })

    elif grant_type == 'client_credentials':
        """
        게이트웨이에서 토큰 획득 용으로 사용
        """
        client_id = request.form.get('client_id')
        client_secret = request.form.get('client_secret')
        try:
            gateway = CampusGateway.objects(id=client_id, secret_key=client_secret).get()
            token = OAuth2AccessToken.create_token('gateway', gateway.id)
            expires_in = token.expires_at - datetime.utcnow()
            expires_in = expires_in.days * 86400 + expires_in.seconds
            return jsonify({
                'access_token': token.access_token,
                'expires_in': expires_in,
                'token_type': 'Bearer',
                'refresh_token': token.refresh_token
            })
        except CampusGateway.DoesNotExist:
            pass
        return jsonify({'error': 'invalid_request'}), 400
    elif grant_type == 'refresh_token':
        return jsonify({'error': 'unsupported_response_type'}), 400
    else:
        return jsonify({'error': 'unsupported_grant_type'}), 400
コード例 #7
0
ファイル: handler.py プロジェクト: qbx2/opencampus
def campus_add_gateway(campus_id):
    if CampusGateway.objects(campus_id=campus_id).count() > 2:
        return redirect(url_for('console.campus_gateways', campus_id=campus_id))

    CampusGateway.create_gateway(campus_id)
    return redirect(url_for('console.campus_gateways', campus_id=campus_id))