-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
nobot.go
59 lines (50 loc) · 1.45 KB
/
nobot.go
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
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
// +build !oss
package admission
import (
"context"
"errors"
"time"
"github.com/drone/drone/core"
)
// ErrCannotVerify is returned when attempting to verify the
// user is a human being.
var ErrCannotVerify = errors.New("Cannot verify user authenticity")
// Nobot enforces an admission policy that restricts access to
// users accounts that were recently created and may be bots.
// The policy expects the source control management system will
// identify and remove the bot accounts before they would be
// eligible to use the system.
func Nobot(service core.UserService, age time.Duration) core.AdmissionService {
return &nobot{service: service, age: age}
}
type nobot struct {
age time.Duration
service core.UserService
}
func (s *nobot) Admit(ctx context.Context, user *core.User) error {
// this admission policy is only enforced for
// new users. Existing users are always admitted.
if user.ID != 0 {
return nil
}
// if the minimum required age is not specified the check
// is skipped.
if s.age == 0 {
return nil
}
account, err := s.service.Find(ctx, user.Token, user.Refresh)
if err != nil {
return err
}
if account.Created == 0 {
return nil
}
now := time.Now()
if time.Unix(account.Created, 0).Add(s.age).After(now) {
return ErrCannotVerify
}
return nil
}