在 Go 中验证 AWS Cognito JWT 令牌
简介
从 JWT 验证和提取信息Amazon Cognito 发行的令牌在 Go 中可能是一个挑战。本文提供了有效处理此任务的简明指南。
先决条件
AWS Cognito 用户必须从 JWKS 终端节点检索公共 JSON Web 密钥 (JWK) 集:
https://cognito-idp.{region}.amazonaws.com/{userPoolId}/.well-known/jwks.json
解析 JWK 并验证JWT
要解析 JWK 集并验证 JWT,请考虑使用:
- [jwk](https://github.com/lestrrat-go/jwx):对于JWK解析
- [jwt-go](https://github.com/dgrijalva/jwt-go):用于JWT验证
实现
- 使用以下命令获取并解析 JWK 集jwk.Fetch()。
- 使用 jwt.Parse() 解析 JWT 令牌。
- 从 JWT 标头中提取“kid”字段,以标识 JWK 集中匹配的公钥.
- 使用 keySet.LookupKeyID(kid) 检索与“kid”关联的公钥header.
- 解析 JWT token 时将公钥分配给 func(token *jwt.Token) (interface{}, error) 回调。
示例代码
package main import ( "fmt" jwt "github.com/dgrijalva/jwt-go" "github.com/lestrrat-go/jwx/jwk" ) func main() { // Replace with your Cognito token and Cognito JWKS endpoint tokenString := "YOUR_JWT_TOKEN" endpoint := "YOUR_COGNITO_JWKS_ENDPOINT" keySet, err := jwk.Fetch(endpoint) if err != nil { fmt.Println(err) return } // Parse the JWT token and validate its signature using the public key token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { if _, ok := token.Method.(*jwt.SigningMethodRS256); !ok { return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) } kid, ok := token.Header["kid"].(string) if !ok { return nil, errors.New("kid header not found") } keys := keySet.LookupKeyID(kid) if !ok { return nil, fmt.Errorf("key with specified kid is not present in jwks") } var publickey interface{} err = keys.Raw(&publickey) if err != nil { return nil, fmt.Errorf("could not parse pubkey") } return publickey, nil }) if err != nil { fmt.Println(err) return } // Access the claims from the validated JWT token claims := token.Claims.(jwt.MapClaims) fmt.Println("User ID: ", claims["sub"]) }
结论
通过利用 jwk 和 jwt-go 等库,开发人员可以在 Go 中高效地验证和检索 Cognito JWT 令牌中的数据,提供一种安全、便捷的方式来对应用程序中的用户进行身份验证。
以上是如何在 Go 中验证 AWS Cognito JWT 令牌?的详细内容。更多信息请关注PHP中文网其他相关文章!

在Go中,init函数用于包初始化。1)init函数在包初始化时自动调用,适用于初始化全局变量、设置连接和加载配置文件。2)可以有多个init函数,按文件顺序执行。3)使用时需考虑执行顺序、测试难度和性能影响。4)建议减少副作用、使用依赖注入和延迟初始化以优化init函数的使用。

go'SselectStatementTreamLinesConcurrentProgrambyMultiplexingOperations.1)itallowSwaitingOnMultipleChannEloperations,执行thefirstreadyone.2)theDefirstreadyone.2)thedefefcasepreventlocksbysbysbysbysbysbythoplocktrograpraproxrograpraprocrecrecectefnoopeready.3)

contextancandwaitgroupsarecrucialingoformanaginggoroutineseflect.1)context contextsallowsAllowsAllowsAllowsAllowsAllingCancellationAndDeadLinesAcrossapibiboundaries,确保GoroutinesCanbestoppedGrace.2)WaitGroupsSynChronizeGoroutines,确保Allimizegoroutines,确保AllizeNizeGoROutines,确保AllimizeGoroutines

goisbeneformervicesduetoitssimplicity,效率,androbustConcurrencySupport.1)go'sdesignemphasemphasizessimplicity and效率,Idealformicroservices.2))其ConcconcurnCurnInesSandChannelsOdinesSallessallessallessAlloSalosalOsalOsalOsalOndlingConconcConccompi.3)

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

Golang在并发性上优于C ,而C 在原始速度上优于Golang。1)Golang通过goroutine和channel实现高效并发,适合处理大量并发任务。2)C 通过编译器优化和标准库,提供接近硬件的高性能,适合需要极致优化的应用。

选择Golang的原因包括:1)高并发性能,2)静态类型系统,3)垃圾回收机制,4)丰富的标准库和生态系统,这些特性使其成为开发高效、可靠软件的理想选择。

Golang适合快速开发和并发场景,C 适用于需要极致性能和低级控制的场景。1)Golang通过垃圾回收和并发机制提升性能,适合高并发Web服务开发。2)C 通过手动内存管理和编译器优化达到极致性能,适用于嵌入式系统开发。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

Atom编辑器mac版下载
最流行的的开源编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

禅工作室 13.0.1
功能强大的PHP集成开发环境

WebStorm Mac版
好用的JavaScript开发工具