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