Skip to content

Commit

Permalink
Merge pull request #1700 from dedis/work-fe2-matteo-popcha
Browse files Browse the repository at this point in the history
Popcha on FE2
  • Loading branch information
pierluca authored Nov 30, 2023
2 parents 4fb9bbf + 95411d7 commit a288416
Show file tree
Hide file tree
Showing 24 changed files with 1,234 additions and 40 deletions.
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(
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 @@ -118,7 +118,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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.github.dedis.popstellar.ui.lao.event.eventlist.EventListFragment;
import com.github.dedis.popstellar.ui.lao.event.meeting.MeetingViewModel;
import com.github.dedis.popstellar.ui.lao.event.rollcall.RollCallViewModel;
import com.github.dedis.popstellar.ui.lao.popcha.PoPCHAHomeFragment;
import com.github.dedis.popstellar.ui.lao.popcha.PoPCHAViewModel;
import com.github.dedis.popstellar.ui.lao.socialmedia.SocialMediaHomeFragment;
import com.github.dedis.popstellar.ui.lao.socialmedia.SocialMediaViewModel;
import com.github.dedis.popstellar.ui.lao.token.TokenListFragment;
Expand Down Expand Up @@ -256,6 +258,9 @@ protected boolean openTab(MainMenuTab tab) {
case DIGITAL_CASH:
openDigitalCashTab();
return true;
case POPCHA:
openPoPCHATab();
return true;
case SOCIAL_MEDIA:
openSocialMediaTab();
return true;
Expand Down Expand Up @@ -293,6 +298,11 @@ private void openDigitalCashTab() {
getSupportFragmentManager(), R.id.fragment_digital_cash_home, DigitalCashHomeFragment::new);
}

private void openPoPCHATab() {
setCurrentFragment(
getSupportFragmentManager(), R.id.fragment_popcha_home, PoPCHAHomeFragment::new);
}

private void openSocialMediaTab() {
setCurrentFragment(
getSupportFragmentManager(), R.id.fragment_social_media_home, SocialMediaHomeFragment::new);
Expand Down Expand Up @@ -389,6 +399,12 @@ public static DigitalCashViewModel obtainDigitalCashViewModel(
return digitalCashViewModel;
}

public static PoPCHAViewModel obtainPoPCHAViewModel(FragmentActivity activity, String laoId) {
PoPCHAViewModel popCHAViewModel = new ViewModelProvider(activity).get(PoPCHAViewModel.class);
popCHAViewModel.setLaoId(laoId);
return popCHAViewModel;
}

/**
* Set the current fragment in the container of the activity
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.github.dedis.popstellar.ui.lao;

import androidx.annotation.IdRes;

import com.github.dedis.popstellar.R;

import java.util.Arrays;
import java.util.List;

Expand All @@ -12,6 +10,7 @@ public enum MainMenuTab {
EVENTS(R.id.main_menu_event_list),
SOCIAL_MEDIA(R.id.main_menu_social_media),
DIGITAL_CASH(R.id.main_menu_digital_cash),
POPCHA(R.id.main_menu_popcha),
WITNESSING(R.id.main_menu_witnessing),
TOKENS(R.id.main_menu_tokens),
DISCONNECT(R.id.main_menu_disconnect);
Expand Down
Loading

0 comments on commit a288416

Please sign in to comment.