-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(middleware/auth): 添加 Auth.GetInfo
- Loading branch information
Showing
13 changed files
with
138 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,4 @@ _testmain.go | |
.vscode/ | ||
.idea/ | ||
.zed/ | ||
.vs/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// SPDX-FileCopyrightText: 2024 caixw | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Package mauth middlewares/auth 的私有函数 | ||
package mauth | ||
|
||
import "github.com/issue9/web" | ||
|
||
type keyType int | ||
|
||
const valueKey keyType = 1 | ||
|
||
const AuthorizationHeader = "Authorization" | ||
|
||
// Set 更新 [web.Context] 保存的值 | ||
func Set[T any](ctx *web.Context, val T) { ctx.SetVar(valueKey, val) } | ||
|
||
// Get 获取当前对话关联的信息 | ||
func Get[T any](ctx *web.Context) (val T, found bool) { | ||
if v, found := ctx.GetVar(valueKey); found { | ||
return v.(T), true | ||
} | ||
|
||
var zero T | ||
return zero, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-FileCopyrightText: 2024 caixw | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
package mauth | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/issue9/assert/v4" | ||
"github.com/issue9/mux/v7/types" | ||
"github.com/issue9/web/server" | ||
) | ||
|
||
func TestGetSet(t *testing.T) { | ||
a := assert.New(t, false) | ||
|
||
s, err := server.New("test", "1.0.0", nil) | ||
a.NotError(err).NotNil(s) | ||
|
||
ctx := s.NewContext(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/path", nil), types.NewContext()) | ||
val, found := Get[int](ctx) | ||
a.False(found).Zero(val) | ||
|
||
Set(ctx, 5) | ||
val, found = Get[int](ctx) | ||
a.True(found).Equal(val, 5) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.