Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add memo field to Sep10Challenge.newChallenge() #435

Merged
merged 1 commit into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
}

sourceCompatibility = 1.6
version = '0.33.0'
version = '0.34.0'
group = 'stellar'
jar.enabled = false

Expand Down
66 changes: 53 additions & 13 deletions src/main/java/org/stellar/sdk/Sep10Challenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,19 @@ public static Transaction newChallenge(
return newChallenge(signer, network, clientAccountId, domainName, webAuthDomain, timebounds, "", "");
}

/**
* Returns a valid <a href="https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0010.md#response" target="_blank">SEP 10</a> challenge, for use in web authentication.
*
* @param signer The server's signing account.
* @param network The Stellar network used by the server.
* @param clientAccountId The stellar account belonging to the client.
* @param domainName The <a href="https://en.wikipedia.org/wiki/Fully_qualified_domain_name" target="_blank">fully qualified domain name</a> of the service requiring authentication.
* @param webAuthDomain The fully qualified domain name of the service issuing the challenge.
* @param timebounds The lifetime of the challenge token.
* @param clientDomain The domain of the client application requesting authentication.
* @param clientSigningKey The stellar account listed as the SIGNING_KEY on the client domain's TOML file.
*/
/**
* Returns a valid <a href="https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0010.md#response" target="_blank">SEP 10</a> challenge, for use in web authentication.
*
* @param signer The server's signing account.
* @param network The Stellar network used by the server.
* @param clientAccountId The stellar account belonging to the client.
* @param domainName The <a href="https://en.wikipedia.org/wiki/Fully_qualified_domain_name" target="_blank">fully qualified domain name</a> of the service requiring authentication.
* @param webAuthDomain The fully qualified domain name of the service issuing the challenge.
* @param timebounds The lifetime of the challenge token.
* @param clientDomain The domain of the client application requesting authentication.
* @param clientSigningKey The stellar account listed as the SIGNING_KEY on the client domain's TOML file.
* @param memo The memo of the challenge transaction.
*/
public static Transaction newChallenge(
KeyPair signer,
Network network,
Expand All @@ -69,7 +70,8 @@ public static Transaction newChallenge(
String webAuthDomain,
TimeBounds timebounds,
String clientDomain,
String clientSigningKey
String clientSigningKey,
Memo memo
) throws InvalidSep10ChallengeException {
byte[] nonce = new byte[48];
SecureRandom random = new SecureRandom();
Expand Down Expand Up @@ -99,6 +101,13 @@ public static Transaction newChallenge(
.addOperation(domainNameOperation)
.addOperation(webAuthDomainOperation);

if (memo != null) {
if (!(memo instanceof MemoId)) {
throw new InvalidSep10ChallengeException("only memo type `id` is supported");
}
builder.addMemo(memo);
JakeUrban marked this conversation as resolved.
Show resolved Hide resolved
}

if (!clientSigningKey.isEmpty()) {
if (StrKey.decodeVersionByte(clientSigningKey) != StrKey.VersionByte.ACCOUNT_ID) {
throw new InvalidSep10ChallengeException(clientSigningKey + " is not a valid account id");
Expand All @@ -114,6 +123,32 @@ public static Transaction newChallenge(
return transaction;
}


/**
* Returns a valid <a href="https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0010.md#response" target="_blank">SEP 10</a> challenge, for use in web authentication.
*
* @param signer The server's signing account.
* @param network The Stellar network used by the server.
* @param clientAccountId The stellar account belonging to the client.
* @param domainName The <a href="https://en.wikipedia.org/wiki/Fully_qualified_domain_name" target="_blank">fully qualified domain name</a> of the service requiring authentication.
* @param webAuthDomain The fully qualified domain name of the service issuing the challenge.
* @param timebounds The lifetime of the challenge token.
* @param clientDomain The domain of the client application requesting authentication.
* @param clientSigningKey The stellar account listed as the SIGNING_KEY on the client domain's TOML file.
*/
public static Transaction newChallenge(
KeyPair signer,
Network network,
String clientAccountId,
String domainName,
String webAuthDomain,
TimeBounds timebounds,
String clientDomain,
String clientSigningKey
) throws InvalidSep10ChallengeException {
return newChallenge(signer, network, clientAccountId, domainName, webAuthDomain, timebounds, clientDomain, clientSigningKey, null);
}

/**
* Reads a SEP 10 challenge transaction and returns the decoded transaction envelope and client account ID contained within.
* <p>
Expand Down Expand Up @@ -162,6 +197,11 @@ public static ChallengeTransaction readChallengeTransaction(String challengeXdr,
throw new InvalidSep10ChallengeException("The transaction sequence number should be zero.");
}

Memo memo = transaction.getMemo();
if (memo != null && !(memo instanceof MemoNone || memo instanceof MemoId)) {
throw new InvalidSep10ChallengeException("only memo type `id` is supported");
}

long maxTime = transaction.getTimeBounds().getMaxTime();
long minTime = transaction.getTimeBounds().getMinTime();
if (maxTime == 0L) {
Expand Down