mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-27 00:43:20 +01:00
New API to support multiple accounts per # (FREEBIE)
This commit is contained in:
@@ -51,7 +51,13 @@ public class AccountAuthenticator implements Authenticator<BasicCredentials, Acc
|
||||
public Optional<Account> authenticate(BasicCredentials basicCredentials)
|
||||
throws AuthenticationException
|
||||
{
|
||||
Optional<Account> account = accountsManager.get(basicCredentials.getUsername());
|
||||
AuthorizationHeader authorizationHeader;
|
||||
try {
|
||||
authorizationHeader = AuthorizationHeader.fromUserAndPassword(basicCredentials.getUsername(), basicCredentials.getPassword());
|
||||
} catch (InvalidAuthorizationHeaderException iahe) {
|
||||
return Optional.absent();
|
||||
}
|
||||
Optional<Account> account = accountsManager.get(authorizationHeader.getNumber(), authorizationHeader.getDeviceId());
|
||||
|
||||
if (!account.isPresent()) {
|
||||
return Optional.absent();
|
||||
|
||||
@@ -24,10 +24,28 @@ import java.io.IOException;
|
||||
|
||||
public class AuthorizationHeader {
|
||||
|
||||
private final String user;
|
||||
private final String number;
|
||||
private final long accountId;
|
||||
private final String password;
|
||||
|
||||
public AuthorizationHeader(String header) throws InvalidAuthorizationHeaderException {
|
||||
private AuthorizationHeader(String number, long accountId, String password) {
|
||||
this.number = number;
|
||||
this.accountId = accountId;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public static AuthorizationHeader fromUserAndPassword(String user, String password) throws InvalidAuthorizationHeaderException {
|
||||
try {
|
||||
String[] numberAndId = user.split("\\.");
|
||||
return new AuthorizationHeader(numberAndId[0],
|
||||
numberAndId.length > 1 ? Long.parseLong(numberAndId[1]) : 1,
|
||||
password);
|
||||
} catch (NumberFormatException nfe) {
|
||||
throw new InvalidAuthorizationHeaderException(nfe);
|
||||
}
|
||||
}
|
||||
|
||||
public static AuthorizationHeader fromFullHeader(String header) throws InvalidAuthorizationHeaderException {
|
||||
try {
|
||||
if (header == null) {
|
||||
throw new InvalidAuthorizationHeaderException("Null header");
|
||||
@@ -55,16 +73,18 @@ public class AuthorizationHeader {
|
||||
throw new InvalidAuthorizationHeaderException("Badly formated credentials: " + concatenatedValues);
|
||||
}
|
||||
|
||||
this.user = credentialParts[0];
|
||||
this.password = credentialParts[1];
|
||||
|
||||
return fromUserAndPassword(credentialParts[0], credentialParts[1]);
|
||||
} catch (IOException ioe) {
|
||||
throw new InvalidAuthorizationHeaderException(ioe);
|
||||
}
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return user;
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public long getDeviceId() {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
|
||||
Reference in New Issue
Block a user