Debian Codegraph @

server/auth.go

raw
1 package server
2
3 import (
4 "encoding/base64"
5 "net/http"
6
7 "github.com/gin-gonic/gin"
8 "github.com/henrybear327/go-proton-api"
9 )
10
11 func (s *Server) handlePostAuthInfo() gin.HandlerFunc {
12 return func(c *gin.Context) {
13 var req proton.AuthInfoReq
14
15 if err := c.BindJSON(&req); err != nil {
16 return
17 }
18
19 info, err := s.b.NewAuthInfo(req.Username)
20 if err != nil {
21 _ = c.AbortWithError(http.StatusUnauthorized, err)
22 return
23 }
24
25 c.JSON(http.StatusOK, info)
26 }
27 }
28
29 func (s *Server) handlePostAuth() gin.HandlerFunc {
30 return func(c *gin.Context) {
31 var req proton.AuthReq
32
33 if err := c.BindJSON(&req); err != nil {
34 return
35 }
36
37 clientEphemeral, err := base64.StdEncoding.DecodeString(req.ClientEphemeral)
38 if err != nil {
39 _ = c.AbortWithError(http.StatusBadRequest, err)
40 return
41 }
42
43 clientProof, err := base64.StdEncoding.DecodeString(req.ClientProof)
44 if err != nil {
45 _ = c.AbortWithError(http.StatusBadRequest, err)
46 return
47 }
48
49 auth, err := s.b.NewAuth(req.Username, clientEphemeral, clientProof, req.SRPSession)
50 if err != nil {
51 _ = c.AbortWithError(http.StatusUnauthorized, err)
52 return
53 }
54
55 c.JSON(http.StatusOK, auth)
56 }
57 }
58
59 func (s *Server) handlePostAuthRefresh() gin.HandlerFunc {
60 return func(c *gin.Context) {
61 var req proton.AuthRefreshReq
62
63 if err := c.BindJSON(&req); err != nil {
64 return
65 }
66
67 auth, err := s.b.NewAuthRef(req.UID, req.RefreshToken)
68 if err != nil {
69 _ = c.AbortWithError(http.StatusUnprocessableEntity, err)
70 return
71 }
72
73 c.JSON(http.StatusOK, auth)
74 }
75 }
76
77 func (s *Server) handleDeleteAuth() gin.HandlerFunc {
78 return func(c *gin.Context) {
79 if err := s.b.DeleteSession(c.GetString("UserID"), c.GetString("AuthUID")); err != nil {
80 _ = c.AbortWithError(http.StatusUnauthorized, err)
81 return
82 }
83 }
84 }
85
86 func (s *Server) handleGetAuthSessions() gin.HandlerFunc {
87 return func(c *gin.Context) {
88 sessions, err := s.b.GetSessions(c.GetString("UserID"))
89 if err != nil {
90 _ = c.AbortWithError(http.StatusInternalServerError, err)
91 return
92 }
93
94 c.JSON(http.StatusOK, gin.H{"Sessions": sessions})
95 }
96 }
97
98 func (s *Server) handleDeleteAuthSessions() gin.HandlerFunc {
99 return func(c *gin.Context) {
100 sessions, err := s.b.GetSessions(c.GetString("UserID"))
101 if err != nil {
102 _ = c.AbortWithError(http.StatusInternalServerError, err)
103 return
104 }
105
106 for _, session := range sessions {
107 if session.UID != c.GetString("AuthUID") {
108 if err := s.b.DeleteSession(c.GetString("UserID"), session.UID); err != nil {
109 _ = c.AbortWithError(http.StatusInternalServerError, err)
110 return
111 }
112 }
113 }
114 }
115 }
116
117 func (s *Server) handleDeleteAuthSession() gin.HandlerFunc {
118 return func(c *gin.Context) {
119 if err := s.b.DeleteSession(c.GetString("UserID"), c.Param("authUID")); err != nil {
120 _ = c.AbortWithError(http.StatusInternalServerError, err)
121 return
122 }
123 }
124 }