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

Commit

Permalink
ktlint: Format
Browse files Browse the repository at this point in the history
  • Loading branch information
M3DZIK committed Oct 29, 2023
1 parent 7f7ecb7 commit 3b6f97a
Show file tree
Hide file tree
Showing 33 changed files with 997 additions and 845 deletions.
77 changes: 46 additions & 31 deletions client/src/main/kotlin/dev/medzik/librepass/client/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ class Client(
private val apiURL: String,
private val accessToken: String? = null,
) {
private val httpClient = OkHttpClient.Builder()
.callTimeout(30, TimeUnit.SECONDS)
.build()
private val httpClient =
OkHttpClient.Builder()
.callTimeout(30, TimeUnit.SECONDS)
.build()
private val httpMediaTypeJson = "application/json; charset=utf-8".toMediaType()

// create authorization header if access token is provided
Expand All @@ -51,11 +52,12 @@ class Client(
*/
@Throws(ClientException::class, ApiException::class)
fun get(endpoint: String): String {
val request = Request.Builder()
.url(apiURL + endpoint)
.addHeader("Authorization", authorizationHeader)
.get()
.build()
val request =
Request.Builder()
.url(apiURL + endpoint)
.addHeader("Authorization", authorizationHeader)
.get()
.build()

return executeAndExtractBody(request)
}
Expand All @@ -67,11 +69,12 @@ class Client(
*/
@Throws(ClientException::class, ApiException::class)
fun delete(endpoint: String): String {
val request = Request.Builder()
.url(apiURL + endpoint)
.addHeader("Authorization", authorizationHeader)
.delete()
.build()
val request =
Request.Builder()
.url(apiURL + endpoint)
.addHeader("Authorization", authorizationHeader)
.delete()
.build()

return executeAndExtractBody(request)
}
Expand All @@ -83,14 +86,18 @@ class Client(
* @return response body
*/
@Throws(ClientException::class, ApiException::class)
fun post(endpoint: String, json: String): String {
fun post(
endpoint: String,
json: String
): String {
val body = json.toRequestBody(httpMediaTypeJson)

val request = Request.Builder()
.url(apiURL + endpoint)
.addHeader("Authorization", authorizationHeader)
.post(body)
.build()
val request =
Request.Builder()
.url(apiURL + endpoint)
.addHeader("Authorization", authorizationHeader)
.post(body)
.build()

return executeAndExtractBody(request)
}
Expand All @@ -102,14 +109,18 @@ class Client(
* @return response body
*/
@Throws(ClientException::class, ApiException::class)
fun patch(endpoint: String, json: String): String {
fun patch(
endpoint: String,
json: String
): String {
val body = json.toRequestBody(httpMediaTypeJson)

val request = Request.Builder()
.url(apiURL + endpoint)
.addHeader("Authorization", authorizationHeader)
.patch(body)
.build()
val request =
Request.Builder()
.url(apiURL + endpoint)
.addHeader("Authorization", authorizationHeader)
.patch(body)
.build()

return executeAndExtractBody(request)
}
Expand All @@ -121,14 +132,18 @@ class Client(
* @return response body
*/
@Throws(ClientException::class, ApiException::class)
fun put(endpoint: String, json: String): String {
fun put(
endpoint: String,
json: String
): String {
val body = json.toRequestBody(httpMediaTypeJson)

val request = Request.Builder()
.url(apiURL + endpoint)
.addHeader("Authorization", authorizationHeader)
.put(body)
.build()
val request =
Request.Builder()
.url(apiURL + endpoint)
.addHeader("Authorization", authorizationHeader)
.put(body)
.build()

return executeAndExtractBody(request)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ class AuthClient(apiUrl: String = Server.PRODUCTION) {
private val client = Client(apiUrl)

@Throws(ClientException::class, ApiException::class)
fun register(email: String, password: String, passwordHint: String? = null) {
fun register(
email: String,
password: String,
passwordHint: String? = null
) {
val serverPreLogin = preLogin("")

val passwordHash = computePasswordHash(password, email, serverPreLogin.toArgon2())
Expand All @@ -37,17 +41,18 @@ class AuthClient(apiUrl: String = Server.PRODUCTION) {
// compute shared key
val sharedKey = computeSharedKey(passwordHash.hash, serverPreLogin.serverPublicKey.fromHexString())

val request = RegisterRequest(
email = email,
passwordHint = passwordHint,
sharedKey = sharedKey.toHexString(),
// Argon2id parameters
parallelism = passwordHash.parallelism,
memory = passwordHash.memory,
iterations = passwordHash.iterations,
// Curve25519 public key
publicKey = publicKey.toHexString()
)
val request =
RegisterRequest(
email = email,
passwordHint = passwordHint,
sharedKey = sharedKey.toHexString(),
// Argon2id parameters
parallelism = passwordHash.parallelism,
memory = passwordHash.memory,
iterations = passwordHash.iterations,
// Curve25519 public key
publicKey = publicKey.toHexString()
)

client.post("$API_ENDPOINT/register", JsonUtils.serialize(request))
}
Expand All @@ -59,29 +64,38 @@ class AuthClient(apiUrl: String = Server.PRODUCTION) {
}

@Throws(ClientException::class, ApiException::class)
fun login(email: String, password: String): UserCredentials {
fun login(
email: String,
password: String
): UserCredentials {
val preLoginData = preLogin(email)

val passwordHash = computePasswordHash(
password = password,
email = email,
argon2Function = preLoginData.toArgon2()
)
val passwordHash =
computePasswordHash(
password = password,
email = email,
argon2Function = preLoginData.toArgon2()
)

return login(email, passwordHash, preLoginData)
}

@Throws(ClientException::class, ApiException::class)
fun login(email: String, passwordHash: Argon2Hash, preLogin: PreLoginResponse? = null): UserCredentials {
fun login(
email: String,
passwordHash: Argon2Hash,
preLogin: PreLoginResponse? = null
): UserCredentials {
val serverPublicKey = preLogin?.serverPublicKey ?: preLogin(email).serverPublicKey

val publicKey = X25519.publicFromPrivate(passwordHash.hash)
val sharedKey = computeSharedKey(passwordHash.hash, serverPublicKey.fromHexString())

val request = LoginRequest(
email = email,
sharedKey = sharedKey.toHexString(),
)
val request =
LoginRequest(
email = email,
sharedKey = sharedKey.toHexString(),
)

val responseBody = client.post("$API_ENDPOINT/oauth?grantType=login", JsonUtils.serialize(request))
val response = JsonUtils.deserialize<UserCredentialsResponse>(responseBody)
Expand All @@ -99,11 +113,15 @@ class AuthClient(apiUrl: String = Server.PRODUCTION) {
}

@Throws(ClientException::class, ApiException::class)
fun loginTwoFactor(apiKey: String, code: String) {
val request = TwoFactorRequest(
apiKey = apiKey,
code = code
)
fun loginTwoFactor(
apiKey: String,
code: String
) {
val request =
TwoFactorRequest(
apiKey = apiKey,
code = code
)

client.post("$API_ENDPOINT/oauth?grantType=2fa", JsonUtils.serialize(request))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ class CipherClient(
* @return [IdResponse]
*/
@Throws(ClientException::class, ApiException::class)
fun insert(cipher: Cipher, secretKey: String): IdResponse {
fun insert(
cipher: Cipher,
secretKey: String
): IdResponse {
return insert(
EncryptedCipher(
cipher = cipher,
Expand Down Expand Up @@ -114,7 +117,10 @@ class CipherClient(
* @return [IdResponse]
*/
@Throws(ClientException::class, ApiException::class)
fun update(cipher: Cipher, secretKey: String): IdResponse {
fun update(
cipher: Cipher,
secretKey: String
): IdResponse {
return update(
EncryptedCipher(
cipher = cipher,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ class CollectionClient(
* @return [IdResponse]
*/
@Throws(ClientException::class, ApiException::class)
fun updateCollection(id: UUID, name: String): IdResponse {
fun updateCollection(
id: UUID,
name: String
): IdResponse {
return updateCollection(id.toString(), name)
}

Expand All @@ -86,7 +89,10 @@ class CollectionClient(
* @return [IdResponse]
*/
@Throws(ClientException::class, ApiException::class)
fun updateCollection(id: String, name: String): IdResponse {
fun updateCollection(
id: String,
name: String
): IdResponse {
val request = CreateCollectionRequest(name = name)
val response = client.patch("$API_ENDPOINT/$id", JsonUtils.serialize(request))
return JsonUtils.deserialize(response)
Expand Down
Loading

0 comments on commit 3b6f97a

Please sign in to comment.