cognitex v0.1.0 Cognitex

Module for managing user acounts through AWS Cognito service.

Link to this section Summary

Functions

Gets the specified user by username in a user pool as an administrator. Works on any user

Authenticates registered user with credentials

Changes the password for a specified user in a user pool

Confirms registration of a user

Allows a user to enter a confirmation code to reset a forgotten password

Calling this API causes a message to be sent to the end user with a confirmation code that is required to change the user’s password

Gets the user attributes and metadata for a user by access token

Registers the user in the specified user pool and creates a user name, password, and user attributes defined in AWS Cognito service

Allows a user to update a specific attribute

Link to this section Functions

Link to this function admin_get_user(username)
admin_get_user(String.t()) :: {:ok, map()} | {:error, map()}

Gets the specified user by username in a user pool as an administrator. Works on any user.

Examples

iex> Cognitex.admin_get_user("john.smith@example.com")
{:ok,
  %{
    user_attributes: %{
      email: "john.smith@example.com",
      email_verified: "true",
      family_name: "Smith",
      name: "John",
      sub: "<sub>"
    }
  }
}

iex> Cognitex.admin_get_user("john.smith@example.com")
{:error,
  %{
    message: "User does not exist.",
    status: "UserNotFoundException"
  }
}
Link to this function authenticate(email, password)
authenticate(String.t(), String.t()) :: {:ok, map()} | {:error, map()}

Authenticates registered user with credentials.

Examples

iex> Cognitex.authenticate("john.smith@example.com", "Test123")
{:ok,
  %{
    "AuthenticationResult" => %{
      "AccessToken" => "<jwt_access_token>",
      "ExpiresIn" => 3600,
      "IdToken" => "<jwt_id_token>",
      "RefreshToken" => "<jwt_refresh_token>",
      "TokenType" => "Bearer"
    },
    "ChallengeParameters" => %{}
  }
}

iex> Cognitex.authenticate("john.smith@example.com", "Test123")
{:error,
  %{
    message: "Incorrect username or password.",
    status: "NotAuthorizedException"
  }
}
Link to this function change_password(token, previous_password, proposed_password)
change_password(String.t(), String.t(), String.t()) ::
  {:ok, map()} | {:error, map()}

Changes the password for a specified user in a user pool.

Examples

iex> Cognitex.change_password("<jwt_access_token>", "Test123", "Test321")
{:ok, %{}}

iex> Cognitex.change_password("<jwt_access_token>", "Test123", "Test321")
{:error,
  %{
    message: "Incorrect username or password.",
    status: "NotAuthorizedException"
  }
}
Link to this function confirm(email, confirmation_code)
confirm(String.t(), String.t()) :: {:ok, map()} | {:error, map()}

Confirms registration of a user.

Examples

iex> Cognitex.confirm("john.smith@example.com", "123456")
{:ok, %{}}

iex> Cognitex.confirm("john.smith@example.com", "123456")
{:error,
  %{
    message: "User cannot be confirm. Current status is CONFIRMED",
    status: "NotAuthorizedException"
  }
}
Link to this function confirm_forgot_password(code, username, password)
confirm_forgot_password(String.t(), String.t(), String.t()) ::
  {:ok, map()} | {:error, map()}

Allows a user to enter a confirmation code to reset a forgotten password.

Examples

iex> Cognitex.confirm_forgot_password("123456", "john.smith@example.com", "Test456")
{:ok, %{}}

iex> Cognitex.confirm_forgot_password("123456", "john.smith@example.com", "Test456")
{:error,
  %{
    message: "Invalid code provided, please request a code again.",
    status: "ExpiredCodeException"
  }
}
Link to this function forgot_password(username)
forgot_password(String.t()) :: {:ok, map()} | {:error, map()}

Calling this API causes a message to be sent to the end user with a confirmation code that is required to change the user’s password.

Examples

iex> Cognitex.forgot_password("john.smith@example.com")
{:ok,
  %{
    "CodeDeliveryDetails" => %{
      "AttributeName" => "email",
      "DeliveryMedium" => "EMAIL",
      "Destination" => "j***@e***.co"
    }
  }
}

iex> Cognitex.forgot_password("john.smith@example.com")
{:error,
  %{
    message: "Username/client id combination not found.",
    status: "UserNotFoundException"
  }
}
Link to this function get_user(token)
get_user(String.t()) :: {:ok, map()} | {:error, map()}

Gets the user attributes and metadata for a user by access token.

Examples

iex> Cognitex.get_user("<jwt_access_token>")
{:ok,
  %{
    user_attributes: %{
      email: "john.smith@example.com",
      email_verified: "true",
      family_name: "Smith",
      name: "John",
      sub: "<sub>"
    }
  }
}

iex> Cognitex.get_user("<jwt_access_token>")
{:error,
  %{
    message: "Could not verify signature for Access Token",
    status: "NotAuthorizedException"
  }
}
Link to this function sign_up(email, password, attrs)
sign_up(String.t(), String.t(), [{atom(), String.t()}]) ::
  {:ok, map()} | {:error, map()}

Registers the user in the specified user pool and creates a user name, password, and user attributes defined in AWS Cognito service.

Examples

iex> Cognitex.sign_up("john.smith@example.com", "Test123", name: "John", family_name: "Smith")
{:ok,
  %{
    "CodeDeliveryDetails" => %{
      "AttributeName" => "email",
      "DeliveryMedium" => "EMAIL",
      "Destination" => "j***@e***.co"
    },
    "UserConfirmed" => false,
    "UserSub" => "uuid"
  }
}

iex> Cognitex.sign_up("john.smith@example.com", "Test123", name: "John", family_name: "Smith")
{:error,
  %{
    message: "An account with the given email already exists.",
    status: "UsernameExistsException"
  }
}
Link to this function update_user_attributes(token, attrs)
update_user_attributes(String.t(), [{atom(), String.t()}]) ::
  {:ok, map()} | {:error, map()}

Allows a user to update a specific attribute.

Examples

iex> Cognitex.update_user_attributes("<jwt_access_token>", name: "Jane", family_name: "Doe")
{:ok, %{}}

iex> Cognitex.update_user_attributes("<jwt_access_token>", name: "Jane", family_name: "Doe")
{:error,
  %{
    message: "Invalid Access Token",
    status: "NotAuthorizedException"
  }
}