-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
224 lines (185 loc) · 9.32 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>OAuth2 Framework</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#157878">
<meta name="description" content="Golang OAuth2 Server Framework" />
<meta name="keywords" content="oauth 2.0, oauth2, oauth, golang oauth2,golang oauth 2.0" />
<link rel="stylesheet" href="../css/normalize.css">
<link rel="stylesheet" href="../css/font.css">
<link rel="stylesheet" href="../css/cayman.css">
<link rel="stylesheet" href="../css/github.min.css">
</head>
<body>
<section class="page-header">
<h1 class="project-name">Golang OAuth2 Server Framework</h1>
<h2 class="project-tagline">一个帮助您简单构建OAuth 2.0服务的框架</h2>
<a href="https://github.com/go-oauth2/oauth2" class="btn">View on GitHub</a>
<a href="/" class="btn">English Document</a>
</section>
<section class="main-content">
<h1>1. 开始使用</h1>
<h2>1.1 创建Manager实例</h2>
<blockquote>
<p>import "gopkg.in/oauth2.v3/manage"</p>
</blockquote>
<pre><code class="go">manager := manage.NewManager()</code></pre>
<h3>1.1.1 Manager的配置参数</h3>
<h4>1.1.1.1 SetAuthorizeCodeExp 设置授权码过期时间(默认为10分钟)</h4>
<pre><code class="go">manager.SetAuthorizeCodeExp(time.Minute * 10)</code></pre>
<h4>1.1.1.2 SetAuthorizeCodeTokenCfg 设置授权码模式令牌的配置参数</h4>
<pre><code class="go">
cfg := &manage.Config{
// 访问令牌过期时间(默认为2小时)
AccessTokenExp: time.Hour * 2,
// 更新令牌过期时间(默认为72小时)
RefreshTokenExp: time.Hour * 24 * 3,
// 是否生成更新令牌(默认为true)
IsGenerateRefresh: true,
}
manager.SetAuthorizeCodeTokenCfg(cfg)
</code></pre>
<h4>1.1.1.3 SetImplicitTokenCfg 设置简化模式令牌的配置参数</h4>
<pre><code class="go">
cfg := &manage.Config{
// 访问令牌过期时间(默认为1小时)
AccessTokenExp: time.Hour * 1,
}
manager.SetAuthorizeCodeTokenCfg(cfg)
</code></pre>
<h4>1.1.1.4 SetPasswordTokenCfg 设置密码模式令牌的配置参数</h4>
<pre><code class="go">
cfg := &manage.Config{
// 访问令牌过期时间(默认为2小时)
AccessTokenExp: time.Hour * 2,
// 更新令牌过期时间(默认为7天)
RefreshTokenExp: time.Hour * 24 * 7,
// 是否生成更新令牌(默认为true)
IsGenerateRefresh: true,
}
manager.SetPasswordTokenCfg(cfg)
</code></pre>
<h4>1.1.1.5 SetClientTokenCfg 设置客户端模式令牌的配置参数</h4>
<pre><code class="go">
cfg := &manage.Config{
// 访问令牌过期时间(默认为2小时)
AccessTokenExp: time.Hour * 2,
}
manager.SetClientTokenCfg(cfg)
</code></pre>
<h4>1.1.1.6 SetRefreshTokenCfg 设置更新令牌的配置参数</h4>
<pre><code class="go">
cfg := &manage.Config{
// 是否重新生成更新令牌(默认为false)
IsGenerateRefresh: false,
}
manager.SetRefreshTokenCfg(cfg)
</code></pre>
<h3>1.1.2 Manager的接口映射</h3>
<h4>1.1.2.1 MapTokenModel 映射令牌模型接口</h4>
<blockquote>
<p>import "gopkg.in/oauth2.v3/models"</p>
</blockquote>
<pre><code class="go">manager.MapTokenModel(models.NewToken())</code></pre>
<h4>1.1.2.2 MapAuthorizeGenerate 映射授权码生成接口</h4>
<blockquote>
<p>import "gopkg.in/oauth2.v3/generates"</p>
</blockquote>
<pre><code class="go">manager.MapAuthorizeGenerate(generates.NewAuthorizeGenerate())</code></pre>
<h4>1.1.2.3 MapAccessGenerate 映射访问令牌生成接口</h4>
<blockquote>
<p>import "gopkg.in/oauth2.v3/generates"</p>
</blockquote>
<pre><code class="go">manager.MapAccessGenerate(generates.NewAccessGenerate())</code></pre>
<h4>1.1.2.4 MustTokenStorage 强制映射访问令牌存储接口</h4>
<blockquote>
<p>import "gopkg.in/oauth2.v3/store"</p>
</blockquote>
<pre><code class="go">manager.MustTokenStorage(store.NewMemoryTokenStore())</code></pre>
<h4>1.1.2.5 MapClientStorage 映射客户端信息存储接口</h4>
<blockquote>
<p>客户端信息的存储,需要根据具体的业务场景来确定,这里暂不提供具体实现</p>
</blockquote>
<h2>1.2 创建Server实例</h2>
<blockquote>
<p>import "gopkg.in/oauth2.v3/server"</p>
</blockquote>
<pre><code class="go">srv := server.NewServer(server.NewConfig(), manager)</code></pre>
<h3>1.2.1 Server的配置参数</h3>
<h4>1.2.1.1 SetAllowedResponseType 设置允许的授权请求类型</h4>
<blockquote>
<p>支持的授权类型:Code(授权码),Token(授权令牌)</p>
</blockquote>
<h4>1.2.1.1 SetAllowedGrantType 设置允许的授权模式类型</h4>
<blockquote>
<p>支持的授权模式:AuthorizationCode(授权码模式),PasswordCredentials(密码模式),ClientCredentials(客户端模式),Refreshing(更新令牌)</p>
</blockquote>
<h3>1.2.2 Server的处理函数</h3>
<h4>1.2.2.1 SetClientInfoHandler 获取请求的客户端信息(默认支持:ClientFormHandler,ClientBasicHandler)</h4>
<pre><code class="go">ClientInfoHandler func(r *http.Request) (clientID, clientSecret string, err error)</code></pre>
<h4>1.2.2.2 SetClientAuthorizedHandler 检查是否允许该客户端通过该授权模式请求令牌</h4>
<pre><code class="go">ClientAuthorizedHandler func(clientID string, grant oauth2.GrantType) (allowed bool, err error)</code></pre>
<h4>1.2.2.3 SetClientScopeHandler 检查该客户端所申请的权限范围</h4>
<pre><code class="go">ClientScopeHandler func(clientID, scope string) (allowed bool, err error)</code></pre>
<h4>1.2.2.4 SetUserAuthorizationHandler 获取请求的用户标识</h4>
<pre><code class="go">UserAuthorizationHandler func(w http.ResponseWriter, r *http.Request) (userID string, err error)</code></pre>
<h4>1.2.2.5 SetPasswordAuthorizationHandler 根据请求的用户名和密码获取用户标识</h4>
<pre><code class="go">PasswordAuthorizationHandler func(username, password string) (userID string, err error)</code></pre>
<h4>1.2.2.6 SetRefreshingScopeHandler 检查更新令牌时,更新的权限范围</h4>
<pre><code class="go">RefreshingScopeHandler func(newScope, oldScope string) (allowed bool, err error)</code></pre>
<h4>1.2.2.7 SetResponseErrorHandler 响应错误处理(支持自定义URI及错误明细)</h4>
<pre><code class="go">ResponseErrorHandler func(err error) (re *errors.Response)</code></pre>
<h4>1.2.2.8 SetInternalErrorHandler 内部错误处理</h4>
<pre><code class="go">InternalErrorHandler func(err error)</code></pre>
<h4>1.2.2.9 SetExtensionFieldsHandler 自定义响应令牌的扩展字段</h4>
<pre><code class="go">ExtensionFieldsHandler func(ti oauth2.TokenInfo) (fieldsValue map[string]interface{})</code></pre>
<h4>1.2.2.10 SetAccessTokenExpHandler 自定义访问令牌的过期时间</h4>
<pre><code class="go">AccessTokenExpHandler func(w http.ResponseWriter, r *http.Request) (exp time.Duration, err error)</code></pre>
<h4>1.2.2.11 SetAuthorizeScopeHandler 自定义权限范围</h4>
<pre><code class="go">AuthorizeScopeHandler func(w http.ResponseWriter, r *http.Request) (scope string, err error)</code></pre>
<h2>1.3 创建HTTP监听服务</h2>
<h3>1.3.1 授权请求处理</h3>
<pre><code class="go">
http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
err := srv.HandleAuthorizeRequest(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
})
</code></pre>
<h3>1.3.2 令牌请求处理</h3>
<pre><code class="go">
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
err := srv.HandleTokenRequest(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
})
</code></pre>
<footer class="site-footer">
<span class="site-footer-credits">This page was generated by <a href="https://pages.github.com">GitHub Pages</a> using the <a href="https://github.com/jasonlong/cayman-theme">Cayman theme</a> by <a href="https://twitter.com/jasonlong">Jason Long</a>.</span>
</footer>
</section>
<script src="../js/highlight.min.js"></script>
<script>
hljs.initHighlightingOnLoad();
</script>
<script>
(function(i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function() {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-67900219-3', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>