LoginFromRequest 供面板 handler 调用,携带 IP 用于暴力破解防护。
(r *http.Request, username, password string)
| 196 | |
| 197 | // LoginFromRequest 供面板 handler 调用,携带 IP 用于暴力破解防护。 |
| 198 | func (m *Manager) LoginFromRequest(r *http.Request, username, password string) (string, error) { |
| 199 | ip := clientIP(r) |
| 200 | if m.isLocked(ip) { |
| 201 | return "", errors.New("too many failed attempts, try later") |
| 202 | } |
| 203 | _, adminUsername, hash, ok := m.adminStore.GetAdminUser() |
| 204 | if !ok || username != adminUsername { |
| 205 | m.recordFailure(ip) |
| 206 | return "", errors.New("invalid credentials") |
| 207 | } |
| 208 | if len(password) > 72 { |
| 209 | m.recordFailure(ip) |
| 210 | return "", errors.New("invalid credentials") |
| 211 | } |
| 212 | if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)); err != nil { |
| 213 | m.recordFailure(ip) |
| 214 | return "", errors.New("invalid credentials") |
| 215 | } |
| 216 | m.clearFailures(ip) |
| 217 | token := randomToken() |
| 218 | if err := m.sessions.Create(token, username); err != nil { |
| 219 | return "", err |
| 220 | } |
| 221 | return token, nil |
| 222 | } |
| 223 | |
| 224 | // CreateSession 直接为指定用户名创建 session,供 Discourse SSO 等第三方认证使用。 |
| 225 | func (m *Manager) CreateSession(username string) (string, error) { |
nothing calls this directly
no test coverage detected