From 3e464747d8464f26288bbccd4c722eb2979fdcc0 Mon Sep 17 00:00:00 2001 From: Conrad Hoffmann Date: Wed, 23 Feb 2022 13:42:28 +0100 Subject: [PATCH] Define and use an auth context structure --- auth/imap.go | 7 +++++++ auth/interface.go | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/auth/imap.go b/auth/imap.go index c2681a9..8db8c3f 100644 --- a/auth/imap.go +++ b/auth/imap.go @@ -1,6 +1,7 @@ package auth import ( + "context" "log" "net/http" @@ -54,6 +55,12 @@ func (prov *IMAPProvider) doAuth(next http.Handler, return } + authCtx := AuthContext{ + AuthMethod: "imap", + UserName: user, + } + ctx := context.WithValue(r.Context(), AuthCtxKey, &authCtx) + r = r.WithContext(ctx) next.ServeHTTP(w, r) } diff --git a/auth/interface.go b/auth/interface.go index 39b3d03..e2d1931 100644 --- a/auth/interface.go +++ b/auth/interface.go @@ -4,6 +4,18 @@ import ( "net/http" ) +var AuthCtxKey = &contextKey{"auth"} + +type contextKey struct { + name string +} + +type AuthContext struct { + AuthMethod string + UserName string + // TODO more? +} + // Abstracts the authentication backend for the server. type AuthProvider interface { // Returns HTTP middleware for performing authentication.