SSO Login using EsiPy

Here we’ll see, step by step, how to log in, using EVE Online SSO and EsiPy.

Reminder: This is only a basic example to go through the full login process.
It doesn't use any sort of webserver, database or anything but just manual actions to explain how it works!

 

Step 1 - Creating an application CCP side.

Go to EVE Online Developers, log in and click on Create new application

Fill the fields like this:

  • Name: The name of your app (your choice)
  • Description: A description of your app
  • Connection Type: Choose “Authentication & API Access”
  • Permissions: Add the scope esi-wallet.read_character_wallet.v1 in the right box (you can put everything if you want too)
  • Callback URL: Put http://localhost:65432/callback/ (it doesn’t matter actually, it’s just to have something)

Then click on Create Application

On the next page, you’ll see your application you just created. Click on the View Application button.

Here you can find the following information we’ll need after:

  • Client ID
  • Secret Key
  • Callback URL

 

Step 2 - EsiPy initialization

If you didn’t already create a virtualenv and install EsiPy, here are the steps to follow:

mkdir esipy_sso_example
cd esipy_sso_example
virtualenv venv
source venv/bin/activate
pip install esipy

Now, in the python console, we’ll create the App, EsiClient and EsiSecurity:

from esipy import EsiApp
from esipy import EsiClient
from esipy import EsiSecurity

app = EsiApp().get_latest_swagger

# replace the redirect_uri, client_id and secret_key values
# with the values you get from the STEP 1 !
security = EsiSecurity(
    redirect_uri='callback URL',
    client_id='you client id',
    secret_key='the_secret_key',
    headers={'User-Agent': 'Something CCP can use to contact you and that define your app'},
)

# and the client object, replace the header user agent value with something reliable !
client = EsiClient(
    retry_requests=True,
    headers={'User-Agent': 'Something CCP can use to contact you and that define your app'},
    security=security
)

Once this is done, we can try to log a user in !

 

Step 3 - Log a user in

To log a user, we will need to go to CCP SSO Login form, log in, then we will be redirected to our callback URL with a code we can use.

Login and get the code

# this print a URL where we can log in
print security.get_auth_uri(state='SomeRandomGeneratedState', scopes=['esi-wallet.read_character_wallet.v1'])

The URL will have the form of
https://login.eveonline.com/oauth/authorize?response_type=code&redirect_uri=[CALLBACK URL]&client_id=[CLIENT ID]&scope=esi-wallet.read_character_wallet.v1

Copy the URL and paste it in your browser.
You will be prompted to log in, accept the scope then redirected to our callback.

Of course, you will get a 404 error, BUT check the URL: you should notice a ?=code=xxxxxxxx within.
Get this code, all of it, we’ll use it very soon.

 

Step 4 - Use the code and get the tokens

Now with the code, we can get our tokens:

# YOUR_CODE is the code you got from Step 3. (do not forget quotes around it)
tokens = security.auth('YOUR_CODE')

The tokens variable now contains your access token, refresh token, and the seconds left until expiry.
By doing this, your security object also knows these tokens and will use them automatically when doing requests

print tokens

{
  "access_token": "frenafeifafrbaefluerbfeainb.tgzggtggtz5fgtz541fra34faerfa.gtgzeg5gt",
  "token_type": "Bearer",
  "expires_in": 1200,
  "refresh_token": "fera48ftea4at64fr684fae"
}

You can save these values somewhere if you want (and then they can be used in the refresh token example).

 

Step 5 - Using the auth

Now that you are authed, we can do a real request on the ESI API:

# use JWT to verify the token and get some basic informations
# this will return an exception if any error happens
api_info = security.verify()

# api_info contains data like this (btw, these have been edited for the example)
#{
#  "sub": "CHARACTER:EVE:964323431",
#  "iss": "login.eveonline.com",
#  "owner": "vfrzifeeefzf68ezFZf=",
#  "jti": "4f86aer4-fr4a-fr44-a57r-4fra64frae64fr",
#  "exp": 1540544567,
#  "azp": "da5638arae1ra57f4ra6f4ra6afc8",
#  "kid": "JWT-Signature-Key",
#  "scp": [
#    "esi-characters.read_blueprints.v1",
#    "esi-characters.read_corporation_roles.v1",
#    "esi-corporations.read_blueprints.v1"
#  ],
#  "name": "Some CharacterName"
#}

# now get the wallet data
op = app.op['get_characters_character_id_wallet'](
    character_id=api_info['sub'].split(':')[-1]
)
wallet = client.request(op)

# and to see the data behind, let's print it
print wallet.data
Now you are done, next step is either see how to use refresh token or see another example using Flask + Flask Login and sqlite as a database.