3 Star 0 Fork 0

Gitee 极速下载/gitlab-workhorsesource

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://gitlab.com/gitlab-org/gitlab-workhorse
克隆/下载
auth_checker.go 1.52 KB
一键复制 编辑 原始数据 按行查看 历史
Nick Thomas 提交于 2016-11-21 19:19 . Handle environments/:id/terminal.ws
package terminal
import (
"errors"
"net/http"
"time"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
)
type AuthCheckerFunc func() *api.TerminalSettings
// Regularly checks that authorization is still valid for a terminal, outputting
// to the stopper when it isn't
type AuthChecker struct {
Checker AuthCheckerFunc
Template *api.TerminalSettings
StopCh chan error
Done chan struct{}
Count int64
}
var ErrAuthChanged = errors.New("Connection closed: authentication changed or endpoint unavailable.")
func NewAuthChecker(f AuthCheckerFunc, template *api.TerminalSettings, stopCh chan error) *AuthChecker {
return &AuthChecker{
Checker: f,
Template: template,
StopCh: stopCh,
Done: make(chan struct{}),
}
}
func (c *AuthChecker) Loop(interval time.Duration) {
for {
select {
case <-time.After(interval):
settings := c.Checker()
if !c.Template.IsEqual(settings) {
c.StopCh <- ErrAuthChanged
return
}
c.Count = c.Count + 1
case <-c.Done:
return
}
}
}
func (c *AuthChecker) Close() error {
close(c.Done)
return nil
}
// Generates a CheckerFunc from an *api.API + request needing authorization
func authCheckFunc(myAPI *api.API, r *http.Request, suffix string) AuthCheckerFunc {
return func() *api.TerminalSettings {
httpResponse, authResponse, err := myAPI.PreAuthorize(suffix, r)
if err != nil {
return nil
}
defer httpResponse.Body.Close()
if httpResponse.StatusCode != http.StatusOK || authResponse == nil {
return nil
}
return authResponse.Terminal
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/gitlab-workhorsesource.git
git@gitee.com:mirrors/gitlab-workhorsesource.git
mirrors
gitlab-workhorsesource
gitlab-workhorsesource
v5.1.0

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385