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

Popcha on FE2 #1700

Merged
merged 12 commits into from
Nov 30, 2023
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.dedis.popstellar.model.network.method.message.data;

import java.util.Objects;
import java.util.*;
import java.util.Objects;

/** Enumerates all possible messages actions */
public enum Action {
Expand Down Expand Up @@ -30,7 +30,8 @@ public enum Action {
DELETE("delete"),
NOTIFY_DELETE("notify_delete"),
KEY("key"),
POST_TRANSACTION("post_transaction");
POST_TRANSACTION("post_transaction"),
AUTH("authenticate");

private static final List<Action> ALL = Collections.unmodifiableList(Arrays.asList(values()));
private final String action;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public enum Objects {
CONSENSUS("consensus"),
CHIRP("chirp"),
REACTION("reaction"),
COIN("coin");
COIN("coin"),
POPCHA("popcha");

private static final List<Objects> ALL = Collections.unmodifiableList(Arrays.asList(values()));
private final String object;
Expand Down Expand Up @@ -62,6 +63,7 @@ public boolean hasToBePersisted() {
return true;
// TODO: add persistence for consensus when it'll have its repo
case "consensus":
case "popcha":
default:
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.github.dedis.popstellar.model.network.method.message.data.popcha;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.github.dedis.popstellar.model.Immutable;
import com.github.dedis.popstellar.model.network.method.message.data.*;
import com.github.dedis.popstellar.model.objects.security.Base64URLData;
import com.google.gson.annotations.SerializedName;

/** Data sent to authenticate to a PoPCHA server */
@Immutable
public class PoPCHAAuthentication extends Data {

@SerializedName("client_id")
private final String clientId;

private final String nonce;
private final Base64URLData identifier;

@SerializedName("identifier_proof")
private final Base64URLData identifierProof;

@Nullable private final String state;

@Nullable
@SerializedName("response_mode")
private final String responseMode;

@SerializedName("popcha_address")
private final String popchaAddress;

public PoPCHAAuthentication(
String clientId,
String nonce,
Base64URLData identifier,
Base64URLData identifierProof,
String popchaAddress,
@Nullable String state,
@Nullable String responseMode) {
this.clientId = clientId;
this.nonce = nonce;
this.identifier = identifier;
this.identifierProof = identifierProof;
this.state = state;
this.responseMode = responseMode;
this.popchaAddress = popchaAddress;
}

public String getClientId() {
return clientId;
}

public String getNonce() {
return nonce;
}

public Base64URLData getIdentifier() {
return identifier;
}

public Base64URLData getIdentifierProof() {
return identifierProof;
}

@Nullable
public String getState() {
return state;
}

@Nullable
public String getResponseMode() {
return responseMode;
}

public String getPopchaAddress() {
return popchaAddress;
}

@Override
public String getObject() {
return Objects.POPCHA.getObject();
}

@Override
public String getAction() {
return Action.AUTH.getAction();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PoPCHAAuthentication that = (PoPCHAAuthentication) o;
return clientId.equals(that.clientId)
&& nonce.equals(that.nonce)
&& identifier.equals(that.identifier)
&& identifierProof.equals(that.identifierProof)
&& java.util.Objects.equals(state, that.state)
&& java.util.Objects.equals(responseMode, that.responseMode)
&& popchaAddress.equals(that.popchaAddress);
}

@Override
public int hashCode() {
return java.util.Objects.hash(
matteosz marked this conversation as resolved.
Show resolved Hide resolved
clientId, nonce, identifier, identifierProof, state, responseMode, popchaAddress);
}

@NonNull
@Override
public String toString() {
return "PoPCHAAuthentication{"
+ "clientId='"
+ clientId
+ '\''
+ ", nonce='"
+ nonce
+ '\''
+ ", identifier='"
+ identifier
+ '\''
+ ", identifierProof='"
+ identifierProof
+ '\''
+ ", state='"
+ state
+ '\''
+ ", responseMode='"
+ responseMode
+ '\''
+ ", popchaAddress='"
+ popchaAddress
+ '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.dedis.popstellar.model.objects.security;

import com.github.dedis.popstellar.model.Immutable;
import com.github.dedis.popstellar.model.objects.security.privatekey.PlainPrivateKey;

/** Represents a AuthToken key pair with its private and public keys */
@Immutable
public class AuthToken extends KeyPair {

public AuthToken(byte[] privateKey, byte[] publicKey) {
super(new PlainPrivateKey(privateKey), new PublicKey(publicKey));
}

public AuthToken(PoPToken otherToken) {
super(otherToken.getPrivateKey(), otherToken.getPublicKey());
}

@Override
public PlainPrivateKey getPrivateKey() {
return (PlainPrivateKey) super.getPrivateKey();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.github.dedis.popstellar.model.objects.security;

import androidx.annotation.NonNull;

import com.github.dedis.popstellar.model.Immutable;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;

Expand Down Expand Up @@ -37,6 +36,10 @@ private static byte[] decode(String data) {
return Base64.getUrlDecoder().decode(data);
}

public static String encode(String data) {
return encode(data.getBytes(StandardCharsets.UTF_8));
}

private static String encode(byte[] data) {
return Base64.getUrlEncoder().encodeToString(data);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.github.dedis.popstellar.model.qrcode;

import android.net.Uri;
import com.github.dedis.popstellar.model.Immutable;
import com.github.dedis.popstellar.utility.MessageValidator;
import java.util.Locale;

@Immutable
public class PoPCHAQRCode {

public static final String FIELD_CLIENT_ID = "client_id";
public static final String FIELD_NONCE = "nonce";
public static final String FIELD_REDIRECT_URI = "redirect_uri";
public static final String FIELD_STATE = "state";
public static final String FIELD_RESPONSE_TYPE = "response_type";
public static final String FIELD_RESPONSE_MODE = "response_mode";
public static final String FIELD_LOGIN_HINT = "login_hint";

private final String clientId;
private final String nonce;
private final String state;
private final String responseMode;
private final String host;

public PoPCHAQRCode(String data, String laoId) throws IllegalArgumentException {
MessageValidator.verify().isValidPoPCHAUrl(data, laoId);

Uri uri = Uri.parse(data);
clientId = uri.getQueryParameter(FIELD_CLIENT_ID);
nonce = uri.getQueryParameter(FIELD_NONCE);
state = uri.getQueryParameter(FIELD_STATE);
responseMode = uri.getQueryParameter(FIELD_RESPONSE_MODE);
final int port = uri.getPort();
host =
String.format(
"%s%s", uri.getHost(), port == -1 ? "" : String.format(Locale.ENGLISH, ":%d", port));
}

public String getClientId() {
return clientId;
}

public String getNonce() {
return nonce;
}

public String getState() {
return state;
}

public String getResponseMode() {
return responseMode;
}

public String getHost() {
return host;
}

@Override
public String toString() {
return "PoPCHAQRCode{"
+ "clientId='"
+ clientId
+ '\''
+ ", nonce='"
+ nonce
+ '\''
+ ", state='"
+ state
+ '\''
+ ", responseMode='"
+ responseMode
+ '\''
+ ", host='"
+ host
+ '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import android.app.Application;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.*;

import com.github.dedis.popstellar.R;
import com.github.dedis.popstellar.model.objects.Wallet;
import com.github.dedis.popstellar.model.objects.view.LaoView;
Expand All @@ -22,18 +20,15 @@
import com.github.dedis.popstellar.utility.error.keys.SeedValidationException;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;

import dagger.hilt.android.lifecycle.HiltViewModel;
import io.reactivex.BackpressureStrategy;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.inject.Inject;

import dagger.hilt.android.lifecycle.HiltViewModel;
import io.reactivex.BackpressureStrategy;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import timber.log.Timber;

@HiltViewModel
Expand Down Expand Up @@ -121,7 +116,7 @@ public void handleData(String data) {
Timber.tag(TAG).e(e, "Invalid QRCode laoData");
Toast.makeText(
getApplication().getApplicationContext(),
R.string.invalid_qrcode_data,
R.string.invalid_qrcode_lao_data,
Toast.LENGTH_LONG)
.show();
return;
Expand Down
Loading
Loading