<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def application_unauthenticated(request, token, state=None, label=None):
 """ An somebody is trying to access an application. """

    application = base.get_application(secret_token=token)
    if application.expires < datetime.datetime.now():
        return render(
            template_name='kgapplications/common_expired.html',
            context={'application': application},
            request=request)

    roles = {'is_applicant', 'is_authorised'}

    # redirect user to real url if possible.
    if request.user.is_authenticated:
        if request.user == application.applicant:
            url = base.get_url(
                request, application, roles, label)
            return HttpResponseRedirect(url)

    state_machine = base.get_state_machine(application)
    return state_machine.process(
        request, application, state, label, roles)