Skip to content

Commit

Permalink
feat(webdav): support anonymous account
Browse files Browse the repository at this point in the history
  • Loading branch information
reruin committed Oct 19, 2021
1 parent b91f0b5 commit 9573c95
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
7 changes: 4 additions & 3 deletions packages/sharelist-webdav/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { parseXML } from './operations/shared'
import { URL } from 'url'

export default (req: http.IncomingMessage, base: string, allows: Array<string>): Context => {
const value = req.headers?.authorization?.split(' ')[1]
const authorization = req.headers?.authorization?.split(' ')[1]

const path = new URL(req.url as string, `http:https://${req.headers.host}`).pathname
const ctx: Context = {
Expand All @@ -14,6 +14,7 @@ export default (req: http.IncomingMessage, base: string, allows: Array<string>):
path: path.replace(base, ''),
base,
config: {},
auth: { user: undefined, pass: undefined },
allows,
get(field: string): string | undefined {
const req: http.IncomingMessage = this.req
Expand All @@ -26,8 +27,8 @@ export default (req: http.IncomingMessage, base: string, allows: Array<string>):
}
}
}
if (value) {
const pairs = Buffer.from(value, "base64").toString("utf8").split(':')
if (authorization) {
const pairs = Buffer.from(authorization, "base64").toString("utf8").split(':')
ctx.auth = { user: pairs[0], pass: pairs[1] }
}
return ctx
Expand Down
32 changes: 17 additions & 15 deletions packages/sharelist-webdav/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

import { WebDAVMethod, WebDAVRequest, Driver, DriverMethod, Context, Response } from "./types"
import { WebDAVMethod, WebDAVRequest, Driver, DriverMethod, Context, Response, StatusCodes } from "./types"
import Commands from './operations/commands'
import { default as createContext } from './context'

interface WebDAVAuth {
(user: string, pass: string): boolean
(user: string | undefined, pass: string | undefined): boolean,
}
export type WebDAVServerOptions = {
driver?: Driver,
base?: string,
auth?: WebDAVAuth,
auth: WebDAVAuth,
redirect: boolean
}

Expand All @@ -27,7 +27,7 @@ export class WebDAVServer {

protected base: string

protected auth: WebDAVAuth | undefined
protected auth: WebDAVAuth

protected config: Record<string, any>

Expand All @@ -50,25 +50,27 @@ export class WebDAVServer {

async request(req: WebDAVRequest): Promise<Response> {
const ctx: Context = createContext(req, this.base, this.allows)
if (this.auth) {
if (!ctx.auth || !this.auth(ctx.auth.user, ctx.auth.pass) === true) {
return {
headers: {
'WWW-Authenticate': `Basic realm="ShareList WebDAV"`
},
status: '401'
}
if (
!(ctx.method == 'options' && !ctx.path) &&
!this.auth(ctx.auth.user, ctx.auth.pass)
) {
return {
headers: {
'X-WebDAV-Status': '401 ' + StatusCodes[401],
'WWW-Authenticate': `Basic realm="ShareList WebDAV"`
},
status: '401'
}
}

ctx.driver = this.driver
ctx.config = this.config
const method = this.methods[ctx.method] || this.unknownMethod

const res = await method(ctx)
const res: Response = await method(ctx)
res.headers ||= {}
if (res.status) {
res.headers ||= {}
// res.headers['X-WebDAV-Status'] = res.status
// res.headers['X-WebDAV-Status'] = res.status + ' ' + StatusCodes[res.status]
}
return res
}
Expand Down
4 changes: 2 additions & 2 deletions packages/sharelist-webdav/src/operations/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { parseXML } from './shared'
import { Context, Response } from '../types'

const filterHeaders = (headers: Record<string, any>): Record<string, any> => {
const exclude = ['range', 'accept-encoding']
const effectFields = ['range', 'accept-encoding']
const ret: Record<string, any> = {}
Object.keys(headers).filter((i: string) => exclude.includes(i.toLocaleLowerCase())).forEach((key: string) => {
Object.keys(headers).filter((i: string) => effectFields.includes(i.toLocaleLowerCase())).forEach((key: string) => {
ret[key] = headers[key]
})
return ret
Expand Down
2 changes: 1 addition & 1 deletion packages/sharelist-webdav/src/operations/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export const parseXML = async (req: http.IncomingMessage): Promise<any> => {
explicitArray: false,
tagNameProcessors: [processors.stripPrefix]
})
}
}
8 changes: 4 additions & 4 deletions packages/sharelist-webdav/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export type Driver = {
}

export type WebDAVAuthRecord = {
user: string,
pass: string
user: string | undefined,
pass: string | undefined
}
export interface Context {
req: http.IncomingMessage,
Expand All @@ -31,7 +31,7 @@ export interface Context {
driver?: Driver,
allows?: Array<string>,
config: Record<string, any>,
auth?: WebDAVAuthRecord
auth: WebDAVAuthRecord
}


Expand All @@ -45,7 +45,7 @@ export type Response = {
body?: any
}

export const StatusCodes = {
export const StatusCodes: Record<string | number, string> = {
200: 'OK',
201: 'Created',
204: 'No Content',
Expand Down

0 comments on commit 9573c95

Please sign in to comment.