toCredential static method

Credential? toCredential(
  1. Object? o
)

Implementation

static Credential? toCredential(Object? o) {
  if (o == null) return null;
  if (o is Credential) return o;

  if (o is String) {
    var parts = o.split(':');

    var user = parts[0];
    var pass = parts.length > 1 ? parts[1] : null;

    return BasicCredential(user, pass ?? '');
  }
  if (o is List) {
    var list = o.map((e) => '$e').toList();
    var user = list.isNotEmpty ? list[0] : null;
    var pass = list.length > 1 ? list[1] : null;

    if (user != null) {
      return BasicCredential(user, pass ?? '');
    }
  } else if (o is Map) {
    var map = o.asJsonMap;
    var user = map.get<String>('username') ?? map.get<String>('user');
    var pass = map.get<String>('password') ??
        map.get<String>('pass') ??
        map.get<String>('passphrase');
    var bearer = map.get<String>('bearer') ?? map.get<String>('token');

    if (user != null) {
      return BasicCredential(user, pass ?? bearer ?? '');
    }

    if (bearer != null) {
      return BearerCredential(bearer);
    }
  }

  return null;
}