Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
Password login
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Aug 11, 2022
1 parent 2135fe0 commit 570abdf
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 46 deletions.
12 changes: 6 additions & 6 deletions core/src/encrypt_password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ use argon2::{
};

#[tauri::command]
pub fn encrypt_password(param: String) -> String {
let password = param.as_bytes();
pub fn encrypt_password(password: String) -> String {
let salt = SaltString::generate(&mut OsRng);

// Argon2 with default params (Argon2id v19)
let argon2 = Argon2::default();

// Hash password to PHC string ($argon2id$v=19$...)
let password_hash = argon2.hash_password(password, &salt).unwrap().to_string();

println!("{}", password_hash);
let password_hash = argon2
.hash_password(password.as_bytes(), &salt)
.unwrap()
.to_string();

let parsed_hash = PasswordHash::new(&password_hash).unwrap();

assert!(Argon2::default()
.verify_password(password, &parsed_hash)
.verify_password(password.as_bytes(), &parsed_hash)
.is_ok());

password_hash.into()
Expand Down
13 changes: 11 additions & 2 deletions interface/windows/confirm/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { navigate } from "../../libraries/navigate"
import { getSettings } from "../../stores/settings"
import { dialog, invoke } from "@tauri-apps/api"
import { getState, setState } from "../../stores/state"

export const confirmPassword = async () => {
const settings = getSettings()
const input = document.querySelector(".passwordInput").value

const result = await invoke("verify_password", { password: input, hash: settings.security.password })
const result = await invoke("verify_password", { password: input, hash: Buffer.from(settings.security.password, "base64").toString() })

console.log(result)
if (result === true) {
const state = getState()
state.authenticated = true
setState(state)

navigate("codes")
} else {
dialog.message("Passwords don't match! \n\nPlease try again!", { type: "error" })
}
}

export const showPassword = () => {
Expand Down
66 changes: 66 additions & 0 deletions interface/windows/landing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { navigate } from "../../libraries/navigate"
import { generateRandomKey, generateSalt } from "../../libraries/auth"
import { getSettings, setSettings } from "../../stores/settings"
import { getState, setState } from "../../stores/state"
import { dialog, invoke } from "@tauri-apps/api"

export const noPassword = async () => {
const settings = getSettings()
Expand All @@ -21,3 +22,68 @@ export const noPassword = async () => {

navigate("codes")
}

export const requirePassword = () => {
document.querySelector(".requirePassword").style.display = "block"
document.querySelector(".landing").style.display = "none"
}

export const createPassword = async () => {
const settings = getSettings()

const input0 = document.querySelector(".passwordInput0")
const input1 = document.querySelector(".passwordInput1")

if (input0.value !== input1.value) {
return dialog.message("Passwords don't match. \n\nPlease try again!", { type: "error" })
}

if (input0.value.length < 8) {
return dialog.message("Minimum password length is 8 characters. \n\nPlease try again!", { type: "error" })
} else if (input0.value.length > 64) {
return dialog.message("Maximum password length is 64 characters. \n\nPlease try again!", { type: "error" })
}

const password = Buffer.from(await invoke("encrypt_password", { password: input0.value }))

const key = generateSalt()

settings.security.key = key.toString("base64")
settings.security.password = password.toString("base64")
settings.security.requireAuthentication = true

setSettings(settings)

navigate("confirm")
}

export const appController = () => {
const settings = getSettings()
const state = getState()

if (settings.security.requireAuthentication === false) {
state.authenticated = true

setState(state)

navigate("codes")
} else if (settings.security.requireAuthentication === true) {
navigate("confirm")
}
}

export const showPassword = (id: number) => {
const inputState = document.querySelector(`.passwordInput${id}`).getAttribute("type")

if (inputState === "password") {
document.querySelector(`.showPassword${id}`).style.display = "none"
document.querySelector(`.hidePassword${id}`).style.display = "block"

document.querySelector(`.passwordInput${id}`).setAttribute("type", "text")
} else {
document.querySelector(`.showPassword${id}`).style.display = "block"
document.querySelector(`.hidePassword${id}`).style.display = "none"

document.querySelector(`.passwordInput${id}`).setAttribute("type", "password")
}
}
53 changes: 15 additions & 38 deletions interface/windows/landing/landing.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="transparent-900 m-auto mt-40 mb-60 w-3/5 rounded-2xl p-10 text-center">
<div class="transparent-900 landing m-auto mt-40 mb-60 w-3/5 rounded-2xl p-10 text-center">
<h1>Welcome to Authme!</h1>

<div class="mx-auto flex w-4/5 flex-col items-center justify-center rounded-2xl p-10">
Expand All @@ -12,7 +12,7 @@
<h4 data-loc class="mb-3">Require password</h4>
<h5 data-loc class="mb-3">You have to type in a password every time you launch Authme.</h5>

<button class="button requirePassword">
<button class="button" on:click={requirePassword}>
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M13 9l3 3m0 0l-3 3m3-3H8m13 0a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
Expand All @@ -31,26 +31,10 @@
</button>
</div>
</div>
<Details>
<div class="flex justify-between">
<div>
<h4>Looking for more methods?</h4>
<h5>Check out Authme Pro.</h5>
</div>
<div>
<button class="smallButton">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
More information
</button>
</div>
</div>
</Details>
</div>
</div>

<div class="transparent-900 m-auto mt-40 mb-60 hidden w-3/5 rounded-2xl p-10 text-center">
<div class="transparent-900 requirePassword m-auto mt-40 mb-60 hidden w-3/5 rounded-2xl p-10 text-center">
<h1>Require password</h1>

<div class="mx-auto flex w-4/5 flex-col items-center justify-center rounded-2xl p-10">
Expand Down Expand Up @@ -130,7 +114,7 @@
</div>
</div>

<button class="button mx-auto mt-10">
<button class="button mx-auto mt-10" on:click={createPassword}>
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
Expand All @@ -145,7 +129,12 @@
<h5>Go back to the previous page and choose a different login method.</h5>
</div>
<div class="ml-20">
<button class="smallButton">
<button
class="smallButton"
on:click={() => {
location.reload()
}}
>
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M11 15l-3-3m0 0l3-3m-3 3h8M3 12a9 9 0 1118 0 9 9 0 01-18 0z" />
</svg>
Expand All @@ -158,23 +147,11 @@
</div>

<script lang="ts">
import { noPassword } from "./index"
import { appController, createPassword, noPassword, requirePassword, showPassword } from "./index"
import Details from "../../components/details.svelte"
import { onMount } from "svelte"
const showPassword = (id: number) => {
const inputState = document.querySelector(`.passwordInput${id}`).getAttribute("type")
if (inputState === "password") {
document.querySelector(`.showPassword${id}`).style.display = "none"
document.querySelector(`.hidePassword${id}`).style.display = "block"
document.querySelector(`.passwordInput${id}`).setAttribute("type", "text")
} else {
document.querySelector(`.showPassword${id}`).style.display = "block"
document.querySelector(`.hidePassword${id}`).style.display = "none"
document.querySelector(`.passwordInput${id}`).setAttribute("type", "password")
}
}
onMount(() => {
appController()
})
</script>

0 comments on commit 570abdf

Please sign in to comment.