💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › e1b85df51c588dbfb8ae7e476b… captured on 2023-11-04 at 12:01:43. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

Go Back

0 package gmi

1

2 import (

3 "log"

4 "strconv"

5

6 "gemigit/db"

7

8 "github.com/pitr/gig"

9 )

10

11 func CreateToken(c gig.Context, readOnly bool) error {

12

13 user, exist := db.GetUser(c.CertHash())

14 if !exist {

15 return c.NoContent(gig.StatusBadRequest, "Invalid username")

16 }

17

18 token, err := user.CreateToken(readOnly)

19 if err != nil {

20 log.Println(err)

21 return c.NoContent(gig.StatusBadRequest, "Unexpected error")

22 }

23 data := struct {

24 Token string

25 }{

26 Token: token,

27 }

28 return execT(c, "token_new.gmi", data)

29 }

30

31 func CreateWriteToken(c gig.Context) error {

32 return CreateToken(c, false)

33 }

34

35 func CreateReadToken(c gig.Context) error {

36 return CreateToken(c, true)

37 }

38

39 func ListTokens(c gig.Context) error {

40

41 user, exist := db.GetUser(c.CertHash())

42 if !exist {

43 return c.NoContent(gig.StatusBadRequest, "Invalid username")

44 }

45

46 tokens, err := user.GetTokens()

47 if err != nil {

48 log.Println(err)

49 return c.NoContent(gig.StatusBadRequest, "Unexpected error")

50 }

51

52 data := struct {

53 Tokens []db.Token

54 Secure bool

55 }{

56 Tokens: tokens,

57 Secure: user.SecureGit,

58 }

59 return execT(c, "token.gmi", data)

60 }

61

62 func ToggleTokenAuth(c gig.Context) error {

63

64 user, exist := db.GetUser(c.CertHash())

65 if !exist {

66 return c.NoContent(gig.StatusBadRequest, "Invalid username")

67 }

68

69 err := user.ToggleSecure()

70 if err != nil {

71 log.Println(err)

72 return c.NoContent(gig.StatusBadRequest, "Unexpected error")

73 }

74

75 return c.NoContent(gig.StatusRedirectTemporary, "/account/token")

76 }

77

78 func RenewToken(c gig.Context) error {

79

80 user, exist := db.GetUser(c.CertHash())

81 if !exist {

82 return c.NoContent(gig.StatusBadRequest, "Invalid username")

83 }

84

85 id, err := strconv.Atoi(c.Param("token"))

86 if err != nil || user.RenewToken(id) != nil {

87 return c.NoContent(gig.StatusBadRequest, "Invalid token")

88 }

89

90 return c.NoContent(gig.StatusRedirectTemporary, "/account/token")

91 }

92

93 func DeleteToken(c gig.Context) error {

94

95 user, exist := db.GetUser(c.CertHash())

96 if !exist {

97 return c.NoContent(gig.StatusBadRequest, "Invalid username")

98 }

99

100 id, err := strconv.Atoi(c.Param("token"))

101 if err != nil || user.DeleteToken(id) != nil {

102 return c.NoContent(gig.StatusBadRequest, "Invalid token")

103 }

104

105 return c.NoContent(gig.StatusRedirectTemporary, "/account/token")

106 }

107