|
| 1 | +import type * as Config from "effect/Config"; |
| 2 | +import type * as ConfigError from "effect/ConfigError"; |
| 3 | +import type * as Schema from "effect/Schema"; |
| 4 | + |
| 5 | +import * as EffectSchemas from "effect-schemas"; |
| 6 | +import * as Array from "effect/Array"; |
| 7 | +import * as Context from "effect/Context"; |
| 8 | +import * as Effect from "effect/Effect"; |
| 9 | +import * as Layer from "effect/Layer"; |
| 10 | +import * as Redacted from "effect/Redacted"; |
| 11 | + |
| 12 | +import * as NimblebitConfig from "./NimblebitConfig.ts"; |
| 13 | + |
| 14 | +/** |
| 15 | + * @since 1.0.0 |
| 16 | + * @category Auth |
| 17 | + */ |
| 18 | +export class NimblebitAuth extends Context.Tag("NimblebitAuth")< |
| 19 | + NimblebitAuth, |
| 20 | + ( |
| 21 | + | { |
| 22 | + readonly host: "https://sync.nimblebit.com"; |
| 23 | + readonly authKey: Schema.Schema.Type<NimblebitConfig.NimblebitAuthKeySchema>; |
| 24 | + } |
| 25 | + | { |
| 26 | + readonly host: "https://authproxy.tinyburg.app"; |
| 27 | + readonly authKey: Redacted.Redacted<string>; |
| 28 | + } |
| 29 | + ) & { |
| 30 | + readonly sign: (data: string) => Effect.Effect<string, never, never>; |
| 31 | + readonly salt: Effect.Effect<Schema.Schema.Type<EffectSchemas.Number.U32>, never, never>; |
| 32 | + readonly burnbot: Effect.Effect<Schema.Schema.Type<NimblebitConfig.AuthenticatedPlayerSchema>, never, never>; |
| 33 | + } |
| 34 | +>() { |
| 35 | + private static readonly burnbots: Array.NonEmptyReadonlyArray< |
| 36 | + Schema.Schema.Type<NimblebitConfig.AuthenticatedPlayerSchema> |
| 37 | + > = [ |
| 38 | + { |
| 39 | + playerId: NimblebitConfig.PlayerIdSchema.make("BPQSY"), |
| 40 | + playerAuthKey: NimblebitConfig.PlayerAuthKeySchema.make( |
| 41 | + Redacted.make("8dad81ae-2626-41b9-8225-325f4809057f") |
| 42 | + ), |
| 43 | + }, |
| 44 | + { |
| 45 | + playerId: NimblebitConfig.PlayerIdSchema.make("9GV59"), |
| 46 | + playerAuthKey: NimblebitConfig.PlayerAuthKeySchema.make( |
| 47 | + Redacted.make("be61b26e-330b-41e0-ad2f-48eb79dc3bd6") |
| 48 | + ), |
| 49 | + }, |
| 50 | + { |
| 51 | + playerId: NimblebitConfig.PlayerIdSchema.make("9GV2Y"), |
| 52 | + playerAuthKey: NimblebitConfig.PlayerAuthKeySchema.make( |
| 53 | + Redacted.make("efe5f6a3-8cd5-4956-897c-ec1db6c26485") |
| 54 | + ), |
| 55 | + }, |
| 56 | + { |
| 57 | + playerId: NimblebitConfig.PlayerIdSchema.make("9GTYN"), |
| 58 | + playerAuthKey: NimblebitConfig.PlayerAuthKeySchema.make( |
| 59 | + Redacted.make("89f9b90b-4e1e-4b48-af56-df39da7b17a7") |
| 60 | + ), |
| 61 | + }, |
| 62 | + ] as const; |
| 63 | + |
| 64 | + private static readonly getBurnbot = Effect.sync(() => { |
| 65 | + // const index = Math.floor(Math.random() * NimblebitAuth.burnbots.length); |
| 66 | + // return NimblebitAuth.burnbots[index]; |
| 67 | + return this.burnbots[0]; |
| 68 | + }); |
| 69 | + |
| 70 | + private static readonly NodeSalt: Effect.Effect<Schema.Schema.Type<EffectSchemas.Number.U32>, never, never> = |
| 71 | + Effect.map( |
| 72 | + Effect.promise(() => import("node:crypto")), |
| 73 | + (crypto) => EffectSchemas.Number.U32.make(crypto.randomBytes(4).readUInt32BE(0)) |
| 74 | + ); |
| 75 | + |
| 76 | + private static readonly WebSalt: Effect.Effect<Schema.Schema.Type<EffectSchemas.Number.U32>, never, never> = |
| 77 | + Effect.sync(() => { |
| 78 | + const array = new Uint8Array(4); |
| 79 | + crypto.getRandomValues(array); |
| 80 | + const salt = new DataView(array.buffer).getUint32(0, false); |
| 81 | + return EffectSchemas.Number.U32.make(salt); |
| 82 | + }); |
| 83 | + |
| 84 | + private static readonly NodeMD5 = (data: string): Effect.Effect<string, never, never> => |
| 85 | + Effect.map( |
| 86 | + Effect.promise(() => import("node:crypto")), |
| 87 | + (crypto) => crypto.createHash("md5").update(data).digest("hex") |
| 88 | + ); |
| 89 | + |
| 90 | + private static readonly WebMD5 = (data: string): Effect.Effect<string, never, never> => |
| 91 | + Effect.promise(async () => { |
| 92 | + const encoder = new TextEncoder(); |
| 93 | + const encoded = encoder.encode(data); |
| 94 | + const hashBuffer = await crypto.subtle.digest("MD5", encoded); |
| 95 | + const hashArray = Array.fromIterable(new Uint8Array(hashBuffer)); |
| 96 | + return hashArray.map((b) => b.toString(16).padStart(2, "0")).join(""); |
| 97 | + }); |
| 98 | + |
| 99 | + static readonly NodeDirect = ({ |
| 100 | + authKey, |
| 101 | + }: { |
| 102 | + authKey: Schema.Schema.Type<NimblebitConfig.NimblebitAuthKeySchema>; |
| 103 | + }): Layer.Layer<NimblebitAuth, never, never> => |
| 104 | + Layer.succeed(this, { |
| 105 | + authKey, |
| 106 | + host: "https://sync.nimblebit.com", |
| 107 | + salt: NimblebitAuth.NodeSalt, |
| 108 | + burnbot: NimblebitAuth.getBurnbot, |
| 109 | + sign: (data: string) => NimblebitAuth.NodeMD5(data + Redacted.value(authKey)), |
| 110 | + }); |
| 111 | + |
| 112 | + static readonly WebDirect = ({ |
| 113 | + authKey, |
| 114 | + }: { |
| 115 | + authKey: Schema.Schema.Type<NimblebitConfig.NimblebitAuthKeySchema>; |
| 116 | + }): Layer.Layer<NimblebitAuth, never, never> => |
| 117 | + Layer.succeed(this, { |
| 118 | + authKey, |
| 119 | + host: "https://sync.nimblebit.com" as const, |
| 120 | + salt: NimblebitAuth.WebSalt, |
| 121 | + burnbot: NimblebitAuth.getBurnbot, |
| 122 | + sign: (data: string) => NimblebitAuth.WebMD5(data + Redacted.value(authKey)), |
| 123 | + }); |
| 124 | + |
| 125 | + static readonly NodeDirectConfig = ( |
| 126 | + config: Config.Config<Schema.Schema.Type<NimblebitConfig.NimblebitAuthKeySchema>> |
| 127 | + ): Layer.Layer<NimblebitAuth, ConfigError.ConfigError, never> => |
| 128 | + Effect.map(config, (authKey) => NimblebitAuth.NodeDirect({ authKey })).pipe(Layer.unwrapEffect); |
| 129 | + |
| 130 | + static readonly WebDirectConfig = ( |
| 131 | + config: Config.Config<Schema.Schema.Type<NimblebitConfig.NimblebitAuthKeySchema>> |
| 132 | + ): Layer.Layer<NimblebitAuth, ConfigError.ConfigError, never> => |
| 133 | + Effect.map(config, (authKey) => NimblebitAuth.WebDirect({ authKey })).pipe(Layer.unwrapEffect); |
| 134 | + |
| 135 | + static readonly NodeTinyburgAuthProxy = ({ |
| 136 | + authKey, |
| 137 | + }: { |
| 138 | + authKey: Redacted.Redacted<string>; |
| 139 | + }): Layer.Layer<NimblebitAuth, never, never> => |
| 140 | + Layer.succeed(this, { |
| 141 | + authKey, |
| 142 | + salt: NimblebitAuth.NodeSalt, |
| 143 | + burnbot: NimblebitAuth.getBurnbot, |
| 144 | + sign: (data: string) => Effect.succeed(data), |
| 145 | + host: "https://authproxy.tinyburg.app" as const, |
| 146 | + }); |
| 147 | + |
| 148 | + static readonly WebTinyburgAuthProxy = ({ |
| 149 | + authKey, |
| 150 | + }: { |
| 151 | + authKey: Redacted.Redacted<string>; |
| 152 | + }): Layer.Layer<NimblebitAuth, never, never> => |
| 153 | + Layer.succeed(this, { |
| 154 | + authKey, |
| 155 | + salt: NimblebitAuth.WebSalt, |
| 156 | + burnbot: NimblebitAuth.getBurnbot, |
| 157 | + sign: (data: string) => Effect.succeed(data), |
| 158 | + host: "https://authproxy.tinyburg.app" as const, |
| 159 | + }); |
| 160 | +} |
0 commit comments