Exemple #1
0
def sso_callback(request):
    code = request.GET.get('code', None)
    eve_client = EveClient.get_instance()

    # verify token
    esi_security = EveClient.get_esi_security()
    esi_token = esi_security.auth(code)
    esi_character = esi_security.verify()

    # create new token
    new_token = EveToken.objects.get_or_create(
        access_token=esi_token['access_token'],
        refresh_token=esi_token['refresh_token'],
        expires_in=esi_token['expires_in'],
        user=request.user)[0]

    # set scopes M2M
    scopes = EveScope.objects.filter(name__in=esi_character['scp'])
    if scopes.count() != len(esi_character['scp']):
        logger.error(
            f"Whoa there. Somehow we added a scope we don't know about. Pass this to Krypted Developers: \n ${esi_character['scp']}"
        )
    new_token.scopes.set(scopes)

    # find or create character
    if EveCharacter.objects.filter(
            external_id=esi_character['sub'].split(":")[-1]).exists():
        character = EveCharacter.objects.get(
            external_id=esi_character['sub'].split(":")[-1])
        if character.token:
            old_token = character.token
            old_token.delete()
        character.token = new_token
        character.save()
    else:
        character = EveCharacter.objects.create(
            external_id=esi_character['sub'].split(":")[-1],
            name=esi_character['name'],
            token=new_token,
        )

    # if no primary user, set
    if not PrimaryEveCharacterAssociation.objects.filter(
            user=request.user).exists():
        PrimaryEveCharacterAssociation.objects.create(user=request.user,
                                                      character=character)

    return redirect('/')
Exemple #2
0
def sso_callback(request):
    code = request.GET.get('code', None)
    eve_client = EveClient.get_instance()

    # verify token
    esi_security = EveClient.get_esi_security()
    esi_token = esi_security.auth(code)
    esi_character = esi_security.verify()

    # create new token
    new_token = EveToken.objects.get_or_create(
        access_token=esi_token['access_token'],
        refresh_token=esi_token['refresh_token'],
        expires_in=esi_token['expires_in'],
        user=request.user)[0]

    # set scopes M2M
    new_token.scopes.set(EveScope.objects.all())

    # find or create character
    if EveCharacter.objects.filter(
            external_id=esi_character['sub'].split(":")[-1]).exists():
        character = EveCharacter.objects.get(
            external_id=esi_character['sub'].split(":")[-1])
        if character.token:
            old_token = character.token
            old_token.delete()
        character.token = new_token
        character.save()
    else:
        character = EveCharacter.objects.create(
            external_id=esi_character['sub'].split(":")[-1],
            name=esi_character['name'],
            token=new_token,
        )

    # if no primary token, set as primary token
    if not EveToken.objects.filter(user=request.user, primary=True).exists():
        logger.info("Setting primary token as %s for %s" %
                    (esi_character['name'], request.user))
        new_token.primary = True
        new_token.save()

    update_character_corporation.apply_async(args=[character.external_id])

    return redirect('app-dashboard')  # TODO: Redirect to EVE Character view