Пример #1
0
def get_key_from_token(request):
    response_content = {}

    try:
        scope = AccessRange.objects.get(key=str(request.GET['scope']))
        authenticator = JSONAuthenticator(scope=scope)
        authenticator.validate(request)
        if request.GET.get('hostid'):
            u2u = get_object_or_404(UserToUser,id=request.GET['hostid'])
            #a request from a peer
            role_list = []
            role_list.append(u2u.role)
            response_content['roles'] = role_list
            response_content['request_type'] = "peer"
        else:
            response_content['request_type'] = "self"
            #a request from self (host=guest)
            response_content['key']=authenticator.user.get_profile().uuid
            response_content['pds_location']=authenticator.user.get_profile().pds_location
            response_content['status']="success"
    except Exception as e:
        response_content['status']="error"
        response_content['message']="failed to get key from token:"
        print e

    return HttpResponse(json.dumps(response_content), mimetype="application/json")
Пример #2
0
def email(request):
    authenticator = JSONAuthenticator()
    try:
        authenticator.validate(request)
    except AuthenticationException:
        return authenticator.error_response()
    return authenticator.response({"email":authenticator.user.email})    
Пример #3
0
def date_joined(request):
    scope = AccessRange.objects.get(key="date_joined")
    authenticator = JSONAuthenticator(scope=scope)
    try:
        authenticator.validate(request)
    except AuthenticationException:
        return authenticator.error_response()
    return authenticator.response({
        "date_joined":str(authenticator.user.date_joined)})
Пример #4
0
 def wrapped_view(*args, **kwargs):
     request = args[0]
     authenticator = JSONAuthenticator(scope=self.scopes)
     try:
         authenticator.validate(request)
         request.user = authenticator.user
     except AuthenticationException:
         return authenticator.error_response()
     return view_func(*args, **kwargs)
Пример #5
0
def last_login(request):
    scope = AccessRange.objects.get(key="last_login")
    authenticator = JSONAuthenticator(scope=scope)
    try:
        authenticator.validate(request)
    except AuthenticationException:
        return authenticator.error_response()
    data = {"date_joined":str(request.user.date_joined)}
    return authenticator.response({
        "last_login":str(authenticator.user.last_login)})
Пример #6
0
def email(request):
    print "in email"
    authenticator = JSONAuthenticator()
    try:
        authenticator.validate(request)
    except AuthenticationException:
        return authenticator.error_response()
    response = authenticator.response({"email": authenticator.user.email})
    print "returning ", response
    return response
Пример #7
0
def last_login(request):
    print "in last_login"
    scope = AccessRange.objects.get(key="last_login")
    authenticator = JSONAuthenticator(scope=scope)
    try:
        authenticator.validate(request)
    except AuthenticationException:
        return authenticator.error_response()
    response = authenticator.response(
        {"last_login": str(authenticator.user.last_login)})
    print "returning ", response
    return response
Пример #8
0
def userinfo(request):
    scope = AccessRange.objects.get(key="funf_write")
    authenticator = JSONAuthenticator(scope=scope)
    try:
        # Validate the request.
        authenticator.validate(request)
    except AuthenticationException as e:
        # Return an error response.
        print e
        return authenticator.error_response("You didn't authenticate.")
    profile = authenticator.user.get_profile()
    response_dict = {}
    response_dict['id'] = profile.uuid
    response_dict['email'] = profile.user.email
    response_dict['name'] = profile.user.username
    response_dict['pds_location'] = 'http://'+str(profile.pds_location)

    return HttpResponse(json.dumps(response_dict), content_type='application/json')
Пример #9
0
def whoami(request):
    """Test authentication"""
    authenticator = JSONAuthenticator()
    try:
        authenticator.validate(request)
    except AuthenticationException:
        return authenticator.error_response()

    user = authenticator.access_token.user
    return authenticator.response({
        "user":
        user.username,
        "url":
        user.get_profile().get_absolute_url(),
        "email":
        user.email,
        "image_url":
        user.get_profile().image_or_default()
    })
Пример #10
0
def userinfo(request):
    authenticator = JSONAuthenticator()  # XXX: Scope?
    try:
        authenticator.validate(request)
    except AuthenticationException:
        return authenticator.error_response()

    return authenticator.response({
        "email":
        authenticator.user.email,
        "username":
        authenticator.user.username,
        "first_name":
        authenticator.user.first_name,
        "last_name":
        authenticator.user.last_name,
        "is_staff":
        authenticator.user.is_staff,
        "is_active":
        authenticator.user.is_active,
        "is_superuser":
        authenticator.user.is_superuser
    })
Пример #11
0
 def __init__(self, scopes=None):
     self.scopes = scopes
     self.authenticator = JSONAuthenticator(scope=self.scopes)
Пример #12
0
def automatic_error_json(request):
    authenticator = JSONAuthenticator()
    return authenticator.error_response()
Пример #13
0
def data(request):
    '''decrypt funf database files, and upload them to your PDS'''
    result = {}
    connection = None
    token = request.GET['bearer_token']

    if request.method == 'GET':
        template = {'token': token}
        return render_to_response('upload.html', template,
                                  RequestContext(request))
    pds = None
    scope = AccessRange.objects.get(key="funf_write")
    authenticator = JSONAuthenticator(scope=scope)
    try:
        # Validate the request.
        authenticator.validate(request)
    except AuthenticationException as e:
        # Return an error response.
        print e
        return authenticator.error_response(content="You didn't authenticate.")
    profile = authenticator.user.get_profile()
    funf_password = profile.funf_password
    print "Registry set_funf_data for uuid: %s" % profile.uuid

    for filename, file in request.FILES.items():
        try:
            file_path = upload_dir + file.name
            try:
                key = decrypt.key_from_password(str(funf_password))
                write_file(str(file_path), file)
            except Exception as ex:
                print "failed to write file to " + file_path + ".  Please make sure you have write permission to the directory set in settings.SERVER_UPLOAD_DIR"
            dbdecrypt.decrypt_if_not_db_file(file_path, key)
            con = sqlite3.connect(file_path)
            cur = con.cursor()
            cur.execute("select name, value from data")
            inserted = []
            for row in cur:
                name = convert_string(row[0])
                json_insert = clean_keys(json.JSONDecoder().decode(
                    convert_string(row[1])))
                #print json_insert
                # Insert into PDS
                pds_data = {}
                pds_data['time'] = json_insert.get('timestamp')
                pds_data['value'] = json_insert
                pds_data['key'] = name
                insert_pds(profile, token, pds_data)
                #print "inserting row..."
                #print pds_data
                inserted.append(convert_string(json_insert) + '\n')
            result = {'success': True, 'message': ''.join(inserted)}
            #remove_file(file_path)
        except Exception as e:
            print e.message
            print traceback.format_exc()
            result = {'success': False, 'error_message': e.message}
    # It doesn't matter what we return at this point - the phones are just checking the response status
    response_dict = {"status": "success"}
    return HttpResponse(json.dumps(response_dict),
                        content_type='application/json')