原文链接 https://blog.bytebytego.com/i/138541852/top-tips-for-api-security
What is an API key? - 什么是API Key-优快云博客
WebAuthn(Web Authentication API) 是什么?-优快云博客
B站搜“Top 12 Tips For API Security”有视频版本。
12 tips for api sec
目录
2,使用 OAuth2 - (Industry Standard Authorization Protocol行业标准授权协议)
4,分配不同的操作权限给 API Keys (Use Leveled API Keys)
9,去这个网站看风险 Check OWASP API Security Risks
1,使用 HTTPS
加密传输协议,防止窃听(eavesdropping)和中间人攻击(man-in-the-middle)等。
保护敏感信息 (如 API Key, Session Token, User Data )被窥视(prying eyes)。
What is an API key? - 什么是API Key-优快云博客
2,使用 OAuth2 - (Industry Standard Authorization Protocol行业标准授权协议)
不直接共享凭据(credentials)。
允许用户授权第三方应用程序对资源访问进行限制。
一个典型的 OAuth2 流程如下图所示。
1,【Authorize Service】客户端程序(如Mobile App)通过将用户重定向到授权服务器(Authorization Server, 例如Google / Facebook)初始化流程。
2,【Request Permission】用户通过授权服务器(Authorization Server)进行身份验证(Authenticate),(其实就是登录)。
3,【Request Approved】用户同意授权客户端程序(Mobile App)访问特定的资源 。
4,【Permission Granted】授权服务器(Authorization Server)告诉客户端程序(如Mobile App)用户同意了,你可以申请access token了。
5,【Get Access Token】客户端程序(如Mobile App)发起token申请。
6,【Access Token】授权服务器(Authorization Server)返回token。
7,【Request Data】客户端程序(Mobile App)代表用户使用token请求资源服务器(Resource Server)。
8,【Data】资源服务器(Resource Server)验证token,返回数据。
再来看一个具体的例子,创建一个Travel App需要访问Google Calendar查看用户是否有空。(具体步骤同上图。)整个过程Travel App不需要存储用户名和密码。
1,Authorize calendar access
2,Request permission
3,Request approved
4,Permission granted
5,Get access token
6,Access token(to Travel App)
7,Access token (access Resource)
3,使用WebAuthn
WebAuthn(Web Authentication API) 是什么?-优快云博客
4,分配不同的操作权限给 API Keys (Use Leveled API Keys)
What is an API key? - 什么是API Key-优快云博客
5,使用授权 (Authorization)
针对不同的用户分配不同的权限,即使用户通过了认证(Authentication-【authentic】),但只能访问被授权(Authorization-【authorize】)的资源。
常见的模式是Role-based Access Control (aka RBAC)。不同的客户端指定不同的角色(Role)。
6,限流 Rate Limiting
7,分版本 API Versioning
8,白名单 Whitelisting
9,去这个网站看风险 Check OWASP API Security Risks
OWASP API Security Project | OWASP Foundation
10,使用 API Gateway
11,Error Handling
不要打印stack trace 在生产环境,