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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
package server
import (
"encoding/base64"
"net/http"
"github.com/gin-gonic/gin"
"github.com/henrybear327/go-proton-api"
)
func (s *Server) handlePostAuthInfo() gin.HandlerFunc {
return func(c *gin.Context) {
var req proton.AuthInfoReq
if err := c.BindJSON(&req); err != nil {
return
}
info, err := s.b.NewAuthInfo(req.Username)
if err != nil {
_ = c.AbortWithError(http.StatusUnauthorized, err)
return
}
c.JSON(http.StatusOK, info)
}
}
func (s *Server) handlePostAuth() gin.HandlerFunc {
return func(c *gin.Context) {
var req proton.AuthReq
if err := c.BindJSON(&req); err != nil {
return
}
clientEphemeral, err := base64.StdEncoding.DecodeString(req.ClientEphemeral)
if err != nil {
_ = c.AbortWithError(http.StatusBadRequest, err)
return
}
clientProof, err := base64.StdEncoding.DecodeString(req.ClientProof)
if err != nil {
_ = c.AbortWithError(http.StatusBadRequest, err)
return
}
auth, err := s.b.NewAuth(req.Username, clientEphemeral, clientProof, req.SRPSession)
if err != nil {
_ = c.AbortWithError(http.StatusUnauthorized, err)
return
}
c.JSON(http.StatusOK, auth)
}
}
func (s *Server) handlePostAuthRefresh() gin.HandlerFunc {
return func(c *gin.Context) {
var req proton.AuthRefreshReq
if err := c.BindJSON(&req); err != nil {
return
}
auth, err := s.b.NewAuthRef(req.UID, req.RefreshToken)
if err != nil {
_ = c.AbortWithError(http.StatusUnprocessableEntity, err)
return
}
c.JSON(http.StatusOK, auth)
}
}
func (s *Server) handleDeleteAuth() gin.HandlerFunc {
return func(c *gin.Context) {
if err := s.b.DeleteSession(c.GetString("UserID"), c.GetString("AuthUID")); err != nil {
_ = c.AbortWithError(http.StatusUnauthorized, err)
return
}
}
}
func (s *Server) handleGetAuthSessions() gin.HandlerFunc {
return func(c *gin.Context) {
sessions, err := s.b.GetSessions(c.GetString("UserID"))
if err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, gin.H{"Sessions": sessions})
}
}
func (s *Server) handleDeleteAuthSessions() gin.HandlerFunc {
return func(c *gin.Context) {
sessions, err := s.b.GetSessions(c.GetString("UserID"))
if err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
for _, session := range sessions {
if session.UID != c.GetString("AuthUID") {
if err := s.b.DeleteSession(c.GetString("UserID"), session.UID); err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
}
}
}
func (s *Server) handleDeleteAuthSession() gin.HandlerFunc {
return func(c *gin.Context) {
if err := s.b.DeleteSession(c.GetString("UserID"), c.Param("authUID")); err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
}
|