1- import { ConfigService } from "@/effect/config-service "
2- import { Config , Context , Effect , Encoding , Layer , Option , Redacted } from "effect"
1+ import { ServerAuth } from "@/server/auth "
2+ import { Effect , Encoding , Layer , Redacted } from "effect"
33import { HttpRouter , HttpServerRequest , HttpServerResponse } from "effect/unstable/http"
44import { HttpApiError , HttpApiMiddleware , HttpApiSecurity } from "effect/unstable/httpapi"
55
@@ -18,41 +18,18 @@ export class Authorization extends HttpApiMiddleware.Service<Authorization>()(
1818 } ,
1919) { }
2020
21- export class ServerAuthConfig extends ConfigService . Service < ServerAuthConfig > ( ) (
22- "@opencode/ExperimentalHttpApiServerAuthConfig" ,
23- {
24- password : Config . string ( "OPENCODE_SERVER_PASSWORD" ) . pipe ( Config . option ) ,
25- username : Config . string ( "OPENCODE_SERVER_USERNAME" ) . pipe ( Config . withDefault ( "opencode" ) ) ,
26- } ,
27- ) { }
28-
2921function validateCredential < A , E , R > (
3022 effect : Effect . Effect < A , E , R > ,
31- credential : { readonly username : string ; readonly password : Redacted . Redacted } ,
32- config : Context . Service . Shape < typeof ServerAuthConfig > ,
23+ credential : ServerAuth . DecodedCredentials ,
24+ config : ServerAuth . Info ,
3325) {
3426 return Effect . gen ( function * ( ) {
35- if ( ! isAuthRequired ( config ) ) return yield * effect
36- if ( ! isCredentialAuthorized ( credential , config ) ) return yield * new HttpApiError . Unauthorized ( { } )
27+ if ( ! ServerAuth . required ( config ) ) return yield * effect
28+ if ( ! ServerAuth . authorized ( credential , config ) ) return yield * new HttpApiError . Unauthorized ( { } )
3729 return yield * effect
3830 } )
3931}
4032
41- function isAuthRequired ( config : Context . Service . Shape < typeof ServerAuthConfig > ) {
42- return Option . isSome ( config . password ) && config . password . value !== ""
43- }
44-
45- function isCredentialAuthorized (
46- credential : { readonly username : string ; readonly password : Redacted . Redacted } ,
47- config : Context . Service . Shape < typeof ServerAuthConfig > ,
48- ) {
49- return (
50- Option . isSome ( config . password ) &&
51- credential . username === config . username &&
52- Redacted . value ( credential . password ) === config . password . value
53- )
54- }
55-
5633function decodeCredential ( input : string ) {
5734 const emptyCredential = {
5835 username : "" ,
@@ -78,11 +55,11 @@ function decodeCredential(input: string) {
7855
7956function validateRawCredential < A , E , R > (
8057 effect : Effect . Effect < A , E , R > ,
81- credential : { readonly username : string ; readonly password : Redacted . Redacted } ,
82- config : Context . Service . Shape < typeof ServerAuthConfig > ,
58+ credential : ServerAuth . DecodedCredentials ,
59+ config : ServerAuth . Info ,
8360) {
84- if ( ! isAuthRequired ( config ) ) return effect
85- if ( ! isCredentialAuthorized ( credential , config ) )
61+ if ( ! ServerAuth . required ( config ) ) return effect
62+ if ( ! ServerAuth . authorized ( credential , config ) )
8663 return Effect . succeed (
8764 HttpServerResponse . empty ( {
8865 status : UNAUTHORIZED ,
@@ -94,8 +71,8 @@ function validateRawCredential<A, E, R>(
9471
9572export const authorizationRouterMiddleware = HttpRouter . middleware ( ) (
9673 Effect . gen ( function * ( ) {
97- const config = yield * ServerAuthConfig
98- if ( ! isAuthRequired ( config ) ) return ( effect ) => effect
74+ const config = yield * ServerAuth . Config
75+ if ( ! ServerAuth . required ( config ) ) return ( effect ) => effect
9976
10077 return ( effect ) =>
10178 Effect . gen ( function * ( ) {
@@ -122,7 +99,7 @@ export const authorizationRouterMiddleware = HttpRouter.middleware()(
12299export const authorizationLayer = Layer . effect (
123100 Authorization ,
124101 Effect . gen ( function * ( ) {
125- const config = yield * ServerAuthConfig
102+ const config = yield * ServerAuth . Config
126103 return Authorization . of ( {
127104 basic : ( effect , { credential } ) => validateCredential ( effect , credential , config ) ,
128105 authToken : ( effect , { credential } ) =>
0 commit comments