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

Fix invalid token #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions frontend/src/components/ChallengeInfoDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
class="md-primary md-layout-item card-tag"
>{{ tag }}</md-chip
>
<div v-if="teamKey && !info.solved" class="flag-field">
<div v-if="teamKey && token && !info.solved" class="flag-field">
<md-field>
<md-input v-model="flag" :placeholder="flagFormat"></md-input>
</md-field>
</div>
</md-dialog-content>
<md-dialog-actions>
<md-button
v-if="!loading && teamKey && !info.solved"
v-if="!loading && teamKey && token && !info.solved"
class="md-raised md-accent"
@click="submitFlag"
>{{ $t("submit") }}</md-button
Expand Down Expand Up @@ -56,7 +56,7 @@
</template>

<script>
import { mapState } from "vuex";
import { mapState, mapActions } from "vuex";
import showdown from "showdown";

import { API } from "@/services/api";
Expand Down Expand Up @@ -88,6 +88,13 @@ export default {
})
},
methods: {
...mapActions(["setToken", "setUser", "setRepository"]),
handleNeedAuthentication() {
this.setToken(null);
this.setUser(null);
this.setRepository(null);
this.showMessage(this.$t("invalidToken"));
},
close() {
if (this.$route.params.id) {
this.$router.push("/challenges");
Expand All @@ -104,6 +111,7 @@ export default {
this.loading = true;
this.sendingFlag = true;
const nizkctf = new NIZKCTF(
this.handleNeedAuthentication,
this.token,
this.repository,
config.submissionsRepo,
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/internationalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default {
teamCountries: "Team countries",
submit: "Submit",
fixErrors: "Please fix the following error(s):",
invalidToken: "The github token is invalid. Sign in again",
teamPrivateKey: "Team private key",
verifyingFlag: "Please wait, we are checking the flag against the hash.",
flagFound:
Expand Down Expand Up @@ -133,6 +134,7 @@ export default {
teamCountries: "Países do time",
submit: "Enviar",
fixErrors: "Por favor, corrija o(s) seguinte(s) erro(s):",
invalidToken: "O token do github está inválido. Faça login novamente",
teamPrivateKey: "Chave privada do time",
flagFound:
"Flag encontrada, enviada! Você pode acompanhar o status do seu envio",
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/services/nizkctf/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ export const repoNameHandler = repoName => {
};

export default class GitHub {
constructor(token) {
constructor(token, onNeedAuthentication) {
this.octokit = new Octokit({
auth: token
});

this.octokit.hook.error("request", async error => {
if (error.status === 401) {
onNeedAuthentication();
}

throw error;
});
}

async getUser() {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/services/nizkctf/gitlab.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Gitlab } from "gitlab";

export default class GitLab {
constructor(token) {
constructor(token, onNeedAuthentication) {
this.api = new Gitlab({
oauthToken: token
});
this.onNeedAuthentication = onNeedAuthentication;
}

async getUser() {
Expand Down
11 changes: 9 additions & 2 deletions frontend/src/services/nizkctf/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ import { deployPath } from "../../config";
const repohostDict = { github: GitHub, gitlab: GitLab };

export default class NIZKCTF {
constructor(token, local, upstream, repohost, team = undefined) {
constructor(
onNeedAuthentication,
token,
local,
upstream,
repohost,
team = undefined
) {
if (!repohostDict[repohost]) {
throw new TypeError(`Invalid repohost: ${repohost}`);
}
this.token = token;
this.local = local;
this.upstream = upstream;

this.api = new repohostDict[repohost](this.token);
this.api = new repohostDict[repohost](this.token, onNeedAuthentication);
this.team = team;
}

Expand Down
35 changes: 20 additions & 15 deletions frontend/src/views/AfterLogin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
></md-textarea>
</md-field>
<div class="center">
<md-button class="md-raised md-primary" @click="copyTeamSecret">
{{ $t("copyToClipboard") }}
</md-button>
<md-button class="md-raised md-primary" @click="copyTeamSecret">{{
$t("copyToClipboard")
}}</md-button>
</div>
</md-content>
<md-steppers v-else :md-active-step.sync="active" md-vertical md-linear>
Expand Down Expand Up @@ -107,17 +107,17 @@
</md-field>
<md-list style="height:300px; overflow:scroll">
<md-list-item v-for="item in filteredCountries" :key="item.key">
<md-checkbox v-model="team.countries" :value="item.key">{{
item.name
}}</md-checkbox>
<md-checkbox v-model="team.countries" :value="item.key">
{{ item.name }}
</md-checkbox>
<country-flag :country="item.key" size="normal" />
</md-list-item>
</md-list>
</md-content>
<div style="display:flex; justify-content:center;">
<md-button class="md-raised md-primary" type="submit">
{{ $t("submit") }}
</md-button>
<md-button class="md-raised md-primary" type="submit">{{
$t("submit")
}}</md-button>
</div>
</div>
<div v-else>
Expand All @@ -126,9 +126,9 @@
<md-input v-model="encodedTeamInput" required></md-input>
</md-field>
<div style="display:flex; justify-content:center;">
<md-button class="md-raised md-primary" type="submit">{{
$t("submit")
}}</md-button>
<md-button class="md-raised md-primary" type="submit">
{{ $t("submit") }}
</md-button>
</div>
</div>
</form>
Expand Down Expand Up @@ -220,6 +220,10 @@ export default {
},
methods: {
...mapActions(["setToken", "setUser", "setRepository", "setTeam"]),
handleNeedAuthentication() {
this.setToken(null);
this.showMessage(this.$t("invalidToken"));
},
copyTeamSecret() {
navigator.clipboard
.writeText(this.encodedTeam)
Expand Down Expand Up @@ -288,6 +292,7 @@ export default {
},
async createTeam() {
const nizkctf = new NIZKCTF(
this.handleNeedAuthentication,
this.token,
this.repository,
config.submissionsRepo,
Expand Down Expand Up @@ -396,7 +401,7 @@ export default {
},
async getUser(token) {
if (config.repohost === "github") {
const github = new GitHub(token);
const github = new GitHub(token, this.handleNeedAuthentication);
const data = await github.getUser();
return data;
} else if (config.repohost === "gitlab") {
Expand All @@ -408,7 +413,7 @@ export default {
},
async createFork(token) {
if (config.repohost === "github") {
const github = new GitHub(token);
const github = new GitHub(token, this.handleNeedAuthentication);
const { path } = await github.createFork(config.submissionsRepo);

const branches = await github.listBranches(path);
Expand Down Expand Up @@ -443,7 +448,7 @@ export default {
},
async verifyFork(token) {
if (config.repohost === "github") {
const github = new GitHub(token);
const github = new GitHub(token, this.handleNeedAuthentication);
const { repo } = repoNameHandler(config.submissionsRepo);
try {
const content = await github.getContents(
Expand Down