# Buggy code
def parse(
    cls, token: str, key: str = None, verify: bool = True, algorithm: str = "HS256"
) -> "Jwt":
    try:
        json = jwt.decode(
            token,
            key=key or current_app.config["SECRET_KEY"],
            verify=verify,
            algorithms=algorithm,
            audience=current_app.config["OAUTH2_CLIENT_ID"]
            or current_app.config["SAML2_ENTITY_ID"]
            or absolute_url(),
        )
    except (DecodeError, ExpiredSignature, InvalidAudience):
        raise

    return Jwt(
        iss=json.get("iss", None),
        typ=json.get("typ", None),
        sub=json.get("sub", None),
        aud=json.get("aud", None),
        exp=json.get("exp", None),
        nbf=json.get("nbf", None),
        iat=json.get("iat", None),
        jti=json.get("jti", None),
        name=json.get("name", None),
        preferred_username=json.get("preferred_username", None),
        email=json.get("email", None),
        provider=json.get("provider", None),
        orgs=json.get("orgs", list()),
        groups=json.get("groups", list()),
        roles=json.get("roles", list()),
        scopes=json.get("scope", "").split(
            " "
        ),  # eg. scope='read write' => scopes=['read', 'write']
        email_verified=json.get("email_verified", None),
        picture=json.get("picture", None),
        customers=[json["customer"]]
        if "customer" in json
        else json.get("customers", list()),
    )


# Traceback (psycopg2.errors.UniqueViolation)
Oct 15 10:45:04 influx alertad[28913]: 2020-10-15 10:45:04,542 alerta.app[28919]: [ERROR] duplicate key value violates unique constraint "users_email_key"
DETAIL:  Key (email)=(xxxxxx@xxxxx) already exists.
request_id=7f8eab74-9c03-4d36-a92f-c5c0d82b3c6f ip=80.120.197.114
Traceback (most recent call last):
File "/opt/alerta/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/alerta/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/opt/alerta/lib/python3.7/site-packages/flask_cors/decorator.py", line 128, in wrapped_function
resp = make_response(f(*args, **kwargs))
File "/opt/alerta/lib/python3.7/site-packages/alerta/auth/oidc.py", line 135, in openid
user.create()
File "/opt/alerta/lib/python3.7/site-packages/alerta/models/user.py", line 140, in create
return User.from_db(db.create_user(self))
File "/opt/alerta/lib/python3.7/site-packages/alerta/database/backends/postgres/base.py", line 910, in create_user
return self._insert(insert, vars(user))
File "/opt/alerta/lib/python3.7/site-packages/alerta/database/backends/postgres/base.py", line 1389, in _insert
cursor.execute(query, vars)
File "/opt/alerta/lib/python3.7/site-packages/psycopg2/extras.py", line 327, in execute
return super(NamedTupleCursor, self).execute(query, vars)
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "users_email_key"
DETAIL:  Key (email)=(xxxxxx@xxxxxxxx) already exists.
Oct 15 10:45:04 influx alertad[28913]: 2020-10-15 10:45:04,541 alerta.app[28919]: [DEBUG] ****************************************

INSERT INTO users (id, name, login, password, email, status, roles, attributes,
create_time, last_login, text, update_time, email_verified)
VALUES ('Cil1aWQ9c2hlZGVuaWdnLG91PVBlb3BsZSxkYz1pbnZlbml1bSxkYz1pbxIEbGRhcA', 'xxxxxxx', 'xxxxxxxxx', '', 'xxxxxxxxxx', 'active', ARRAY['admin']
NULL, '', '2020-10-15T10:45:04.541Z', true)
RETURNING *

**************************************** request_id=7f8eab74-9c03-4d36-a92f-c5c0d82b3c6f ip=xxxxx
Oct 15 10:45:04 influx alertad[28913]: 2020-10-15 10:45:04,540 alerta.app[28919]: [DEBUG] ****************************************
SELECT * FROM users WHERE id='Cil1aWQ9c2hlZGVuaWdnLG91PVBlb3BsZSxkYz1pbnZlbml1bSxkYz1pbxIEbGRhcA'
**************************************** request_id=7f8eab74-9c03-4d36-a92f-c5c0d82b3c6f ip=xxxxx

# Fixed code
def parse(
    cls, token: str, key: str = None, verify: bool = True, algorithm: str = "HS256"
) -> "Jwt":
    try:
        json = jwt.decode(
            token,
            key=key or current_app.config["SECRET_KEY"],
            verify=verify,
            algorithms=algorithm,
            audience=current_app.config["OAUTH2_CLIENT_ID"]
            or current_app.config["SAML2_ENTITY_ID"]
            or absolute_url(),
        )
    except (DecodeError, ExpiredSignature, InvalidAudience):
        raise

    return Jwt(
        iss=json.get("iss", None),
        typ=json.get("typ", None),
        sub=json.get("sub", None),
        aud=json.get("aud", None),
        exp=json.get("exp", None),
        nbf=json.get("nbf", None),
        iat=json.get("iat", None),
        jti=json.get("jti", None),
        name=json.get("name", None),
        preferred_username=json.get("preferred_username", None),
        email=json.get("email", None),
        provider=json.get("provider", None),
        orgs=json.get("orgs", list()),
        groups=json.get("groups", list()),
        roles=json.get("roles", list()),
        scopes=json.get("scope", "").split(
            " "
        ),  # eg. scope='read write' => scopes=['read', 'write']
        email_verified=json.get("email_verified", None),
        picture=json.get("picture", None),
        customers=[json["customer"]]
        if "customer" in json
        else json.get("customers", list()),
        oid=json.get("oid"),
    )