Tyk v2.2 Documentation Components

  1. Home
  2. Tyk v2.2 Documentation Components
  3. Create a basic auth user

Create a basic auth user

For a user session object, to enable basic auth, set the relevant fields in the session object:

{
    ...
    "basic_auth_data": {
        "password": "mickey-mouse"
    }
    ...
}

Notice the basic_auth_data section – this is all that is really required, if an API is basic auth enabled, any keys that are retrieved will check this field for a password and compare it to the password encoded in the request. The password will be encrypted by default using bcrypt to ensure it is secure.

Note: Basic authentication keys are not created the same way as other keys, since the key ID is not generated by the system a basic auth key cannot use the /tyk/keys/create endpoint, and instead should POST to /tyk/keys/{username} of the Tyk Gateway API, this will ADD a key to the system. Subsequent requests will overwrite this entry, sending a PUT request will update the entry.

Create user using Gateway API

The below command will use the gateway API to generate a new basic auth user in Tyk Gateway:

curl -X POST -H "x-tyk-authorization: 352d20fe67be67f6340b4c0605b044c3"
 -s
 -H "Content-Type: application/json"
 -X POST
 -d '{
    "allowance": 1000,
    "rate": 1000,
    "per": 1,
    "expires": -1,
    "quota_max": -1,
    "org_id": "53ac07777cbb8c2d53000002",
    "quota_renews": 1449051461,
    "quota_remaining": -1,
    "quota_renewal_rate": 60,
    "access_rights": {
        "{API-ID}": {
            "api_id": "{API-ID}",
            "api_name": "{API-NAME}",
            "versions": ["Default"]
        }
    },
    "meta_data": {},
    "basic_auth_data": {
        "password": "mickey-mouse"
    }
 }'
 http://{your-tyk-gateway-host}:{port}/tyk/keys/testuser | python -mjson.tool

Create user using Dashboard API

The following command will create a basic auth user with the dashboard API:

curl -X POST -H "Authorization: {YOUR API KEY}"
 -s
 -H "Content-Type: application/json"
 -X POST
 -d '{
    "allowance": 1000,
    "rate": 1000,
    "per": 1,
    "expires": -1,
    "quota_max": -1,
    "org_id": "53ac07777cbb8c2d53000002",
    "quota_renews": 1449051461,
    "quota_remaining": -1,
    "quota_renewal_rate": 60,
    "access_rights": {
        "{API-ID}": {
            "api_id": "{API-ID}", 
            "api_name": "{API-NAME}", 
            "versions": [
                "Default"
            ]
        }
    },
    "meta_data": {},
    "basic_auth_data": {
        "password": "mickey-mouse"
    }
 }'
 http://{your-tyk-gateway-host}:{port}/api/apis/keys/basic/mysupertestuser2 | python -mjson.tool

Note: The most important thing to ensure with both of these commands is that the ORG ID is set correctly and consistently.