Future<AuthCode> authenticateForCode(String username, String password, String clientID, { int expirationInSeconds: 600, List<AuthScope> requestedScopes })

Creates a one-time use authorization code for a given client ID and user credentials.

This methods works with this instance's storage to generate and store the authorization code if the credentials are correct. If they are not correct, it will throw the appropriate AuthRequestError.

Source

Future<AuthCode> authenticateForCode(
    String username, String password, String clientID,
    {int expirationInSeconds: 600, List<AuthScope> requestedScopes}) async {
  if (clientID == null) {
    throw new AuthServerException(AuthRequestError.invalidClient, null);
  }

  AuthClient client = await clientForID(clientID);
  if (client == null) {
    throw new AuthServerException(AuthRequestError.invalidClient, null);
  }

  if (username == null || password == null) {
    throw new AuthServerException(AuthRequestError.invalidRequest, client);
  }

  if (client.redirectURI == null) {
    throw new AuthServerException(
        AuthRequestError.unauthorizedClient, client);
  }

  var authenticatable =
      await storage.fetchAuthenticatableByUsername(this, username);
  if (authenticatable == null) {
    throw new AuthServerException(AuthRequestError.accessDenied, client);
  }

  var dbSalt = authenticatable.salt;
  var dbPassword = authenticatable.hashedPassword;
  var hash = AuthUtility.generatePasswordHash(password, dbSalt);
  if (hash != dbPassword) {
    throw new AuthServerException(AuthRequestError.accessDenied, client);
  }

  List<AuthScope> validScopes;
  if (client.supportsScopes) {
    if ((requestedScopes?.length ?? 0) == 0) {
      throw new AuthServerException(AuthRequestError.invalidScope, client);
    }

    validScopes = requestedScopes
        .where((incomingScope) => client.allowsScope(incomingScope))
        .toList();

    if (validScopes.length == 0) {
      throw new AuthServerException(AuthRequestError.invalidScope, client);
    }
  }

  AuthCode authCode =
      _generateAuthCode(authenticatable.id, client, expirationInSeconds, scopes: validScopes);
  await storage.storeAuthCode(this, authCode);
  return authCode;
}