Skip to content

Commit

Permalink
Large Commit #2
Browse files Browse the repository at this point in the history
  • Loading branch information
GirlInPurple committed Jul 31, 2024
1 parent efd0fa4 commit a951f11
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 34 deletions.
35 changes: 20 additions & 15 deletions jsdk/src/main/kotlin/net/onedsix/api/ApiWrapper.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package net.onedsix.api

import com.google.gson.Gson
import okhttp3.Cookie
import okhttp3.CookieJar
Expand All @@ -6,20 +8,23 @@ import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

const val API_URL: String = "https://onedsixapi.shuttleapp.rs/"

/**
* Creates a new instance of the API wrapper.
* @param gsonInstance A null-safe option to give a custom `com.google.gson.Gson` instance for better formatting or to save on memory. Otherwise it uses the default `Gson()`.
* */
class OneDSixApiWrapper(gsonInstance: Gson?) {
class ApiWrapper(gsonInstance: Gson?, homeserver: String?) {
companion object {
/** The built-in base 1D6 homeserver */
private const val API_URL: String = "https://onedsixapi.shuttleapp.rs/"
}

private val gson = gsonInstance ?: Gson()
private val cookieStore = CookieStore()
private val client = OkHttpClient.Builder()
.cookieJar(cookieStore)
.build()
private val retrofit = Retrofit.Builder()
.baseUrl(API_URL)
.baseUrl(homeserver ?: API_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build()
Expand All @@ -31,20 +36,20 @@ class OneDSixApiWrapper(gsonInstance: Gson?) {
fun getCookies(): List<Cookie> {
return cookieStore.getCookies()
}
}

class CookieStore : CookieJar {
private val cookieMap = mutableMapOf<String, List<Cookie>>()
class CookieStore : CookieJar {
private val cookieMap = mutableMapOf<String, List<Cookie>>()

override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
cookieMap[url.host] = cookies
}
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
cookieMap[url.host] = cookies
}

override fun loadForRequest(url: HttpUrl): List<Cookie> {
return cookieMap[url.host] ?: emptyList()
}
override fun loadForRequest(url: HttpUrl): List<Cookie> {
return cookieMap[url.host] ?: emptyList()
}

fun getCookies(): List<Cookie> {
return cookieMap.values.flatten()
fun getCookies(): List<Cookie> {
return cookieMap.values.flatten()
}
}
}
36 changes: 19 additions & 17 deletions jsdk/src/main/kotlin/net/onedsix/api/Authenticator.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package net.onedsix.api

import com.google.gson.annotations.SerializedName
import de.mkammerer.argon2.Argon2
import de.mkammerer.argon2.Argon2Factory
Expand All @@ -6,23 +8,6 @@ import retrofit2.Retrofit
import retrofit2.http.Body
import retrofit2.http.POST

data class CredentialsRequest(
@SerializedName("username") val username: String,
@SerializedName("password") val password: String
)

data class LoginResponse(
@SerializedName("message") val message: String // Assuming the response has a message
)

interface UserApi {
@POST("/v1/user/login")
fun login(@Body request: CredentialsRequest): Call<LoginResponse>

@POST("/v1/user/signup")
fun signup(@Body request: CredentialsRequest): Call<LoginResponse>
}

class Authenticator(retrofit: Retrofit) {
private val argon2: Argon2 = Argon2Factory.create()
private val endpoint: UserApi = retrofit.create(UserApi::class.java)
Expand All @@ -44,4 +29,21 @@ class Authenticator(retrofit: Retrofit) {
null
}
}

data class CredentialsRequest(
@SerializedName("username") val username: String,
@SerializedName("password") val password: String
)

data class LoginResponse(
@SerializedName("message") val message: String
)

interface UserApi {
@POST("/v1/user/login")
fun login(@Body request: CredentialsRequest): Call<LoginResponse>

@POST("/v1/user/signup")
fun signup(@Body request: CredentialsRequest): Call<LoginResponse>
}
}
4 changes: 4 additions & 0 deletions tsdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
"license": "AGPL-3.0-only",
"dependencies": {
"@types/node": "^22.0.1",
"argon2": "^0.40.3",
"argon2-browser": "^1.18.0",
"axios": "^1.7.2",
"axios-cookiejar-support": "^5.0.2",
"tough-cookie": "^4.1.4",
"typescript": "^5.5.4"
},
"devDependencies": {
"@types/argon2-browser": "^1.18.4",
"@types/tough-cookie": "^4.0.5",
"ts-loader": "^9.5.1",
"webpack": "^5.93.0",
"webpack-cli": "^5.1.4"
Expand Down
7 changes: 7 additions & 0 deletions tsdk/src/argon/browserArgon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { hash } from 'argon2-browser'

export default async function hashPassword(password: string): Promise<string> {
const result = await hash({ pass: password, salt: new Uint8Array(16) })
return result.encoded
}

5 changes: 5 additions & 0 deletions tsdk/src/argon/nodeArgon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import argon2 from 'argon2'

export default async function hashPassword(password: string): Promise<string> {
return await argon2.hash(password)
}
16 changes: 15 additions & 1 deletion tsdk/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
let hashPassword: (password: string) => Promise<string>;

if (typeof window !== 'undefined') {
// Browser environment
import('./argon/browserArgon').then(module => {
hashPassword = module.default
})
} else {
// Node.js environment
import('./argon/nodeArgon').then(module => {
hashPassword = module.default
})
}

export default class Authenticator {

}
2 changes: 1 addition & 1 deletion tsdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface RequestOptions extends AxiosRequestConfig { }
export default class ApiSdk {
private homeServer: string
private jar: CookieJar
public authenticated: boolean
public authenticated: boolean = false

public AUTH: Authenticator

Expand Down
14 changes: 14 additions & 0 deletions tsdk/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"lib": ["ES6", "DOM"],
"declaration": true,
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src"]
}

0 comments on commit a951f11

Please sign in to comment.