| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strings" |
| 6 | "time" |
| 7 | |
| 8 | "gitlab.com/Timfa/bluegit/internal/access" |
| 9 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 10 | "gitlab.com/Timfa/bluegit/internal/store" |
| 11 | "golang.org/x/crypto/ssh" |
| 12 | ) |
| 13 | |
| 14 | func (s *Server) opIssueToken(ctx context.Context, user store.User, name string, scopes []string, expires time.Time) (string, store.TokenInfo, error) { |
| 15 | stored, _, err := access.ParseScopes(scopes) |
| 16 | if err != nil { |
| 17 | return "", store.TokenInfo{}, fail(400, err) |
| 18 | } |
| 19 | plain, info, err := s.Store.IssueToken(ctx, user.ID, store.TokenRequest{Name: name, Scopes: stored, ExpiresAt: expires}) |
| 20 | if err != nil { |
| 21 | return "", store.TokenInfo{}, fail(400, err) |
| 22 | } |
| 23 | return plain, info, nil |
| 24 | } |
| 25 | |
| 26 | func (s *Server) opRevokeToken(ctx context.Context, user store.User, id int64) error { |
| 27 | if err := s.Store.DeleteToken(ctx, user.ID, id); err != nil { |
| 28 | return notFound(err) |
| 29 | } |
| 30 | return nil |
| 31 | } |
| 32 | |
| 33 | func (s *Server) opAddSSH(ctx context.Context, user store.User, name, line string) error { |
| 34 | line = strings.TrimSpace(line) |
| 35 | pub, _, _, _, err := ssh.ParseAuthorizedKey([]byte(line)) |
| 36 | if err != nil { |
| 37 | return fail(400, i18n.E("err.sshBad")) |
| 38 | } |
| 39 | stored := strings.TrimSpace(string(ssh.MarshalAuthorizedKey(pub))) |
| 40 | if err := s.Store.AddSSHKey(ctx, user.ID, name, stored, ssh.FingerprintSHA256(pub)); err != nil { |
| 41 | return fail(400, err) |
| 42 | } |
| 43 | return nil |
| 44 | } |
| 45 | |
| 46 | func (s *Server) opRemoveSSH(ctx context.Context, user store.User, id int64) error { |
| 47 | if err := s.Store.DeleteSSHKey(ctx, user.ID, id); err != nil { |
| 48 | return notFound(err) |
| 49 | } |
| 50 | return nil |
| 51 | } |
| 52 | |
| 53 | func (s *Server) opSetLanguage(ctx context.Context, user store.User, raw string) (i18n.Lang, error) { |
| 54 | lang := i18n.Parse(raw) |
| 55 | if err := s.Store.SetLanguage(ctx, user.ID, string(lang)); err != nil { |
| 56 | return lang, fail(400, err) |
| 57 | } |
| 58 | return lang, nil |
| 59 | } |
| 60 | |
| 61 | func (s *Server) opExport(ctx context.Context, u store.User) map[string]any { |
| 62 | type secretRow struct { |
| 63 | Scope string `json:"scope"` |
| 64 | Name string `json:"name"` |
| 65 | Value string `json:"value"` |
| 66 | } |
| 67 | var secrets []secretRow |
| 68 | ids, _ := s.Store.ListRepoIDs(ctx, u.ID) |
| 69 | for _, id := range ids { |
| 70 | role, err := s.Store.EffectiveRole(ctx, u.ID, id) |
| 71 | if err != nil || !role.CanManageSecrets() { |
| 72 | continue |
| 73 | } |
| 74 | repo, err := s.Store.RepoByID(ctx, id) |
| 75 | if err != nil { |
| 76 | continue |
| 77 | } |
| 78 | vals, err := s.Store.SecretValues(ctx, "repo", id, true) |
| 79 | if err != nil { |
| 80 | continue |
| 81 | } |
| 82 | for name, value := range vals { |
| 83 | secrets = append(secrets, secretRow{Scope: "repo:" + repo.FullPath(), Name: name, Value: value}) |
| 84 | } |
| 85 | } |
| 86 | groups, _ := s.Store.ListGroups(ctx, u.ID) |
| 87 | for _, g := range groups { |
| 88 | role, err := s.Store.GroupRole(ctx, g.ID, u.ID) |
| 89 | if err != nil || !role.AtLeast(access.RoleMaintainer) { |
| 90 | continue |
| 91 | } |
| 92 | vals, err := s.Store.SecretValues(ctx, "group", g.ID, true) |
| 93 | if err != nil { |
| 94 | continue |
| 95 | } |
| 96 | for name, value := range vals { |
| 97 | secrets = append(secrets, secretRow{Scope: "group:" + g.Path, Name: name, Value: value}) |
| 98 | } |
| 99 | } |
| 100 | tokens, _ := s.Store.ListTokens(ctx, u.ID) |
| 101 | keys, _ := s.Store.ListSSHKeys(ctx, u.ID) |
| 102 | return map[string]any{ |
| 103 | "exported_at": time.Now().UTC().Format(time.RFC3339), |
| 104 | "account": map[string]any{ |
| 105 | "id": u.AuthID, "username": u.Username, "avatar_url": u.AvatarURL, "instance_admin": u.InstanceAdmin, |
| 106 | }, |
| 107 | "tokens": tokens, "ssh_keys": keys, "secrets": secrets, |
| 108 | } |
| 109 | } |