-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f459f2
commit 1869b83
Showing
30 changed files
with
420 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package auth | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
func Auth(c *gin.Context) { | ||
passcode := os.Getenv("VORTEXNOTES_PASSCODE") | ||
|
||
type RequestData struct { | ||
Passcode string `json:"passcode"` | ||
} | ||
|
||
var requestData RequestData | ||
|
||
if err := c.BindJSON(&requestData); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) | ||
return | ||
} | ||
|
||
if requestData.Passcode != passcode { | ||
c.JSON(http.StatusBadRequest, gin.H{"message": "ePasscode Invalid"}) | ||
return | ||
} | ||
|
||
authScopes := os.Getenv("VORTEXNOTES_AUTH_SCOPE") | ||
if authScopes == "" { | ||
authScopes = "show,create,edit,delete" | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"auth_scope": authScopes, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package configuration | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
func Config(c *gin.Context) { | ||
needAuthScopes := os.Getenv("VORTEXNOTES_AUTH_SCOPE") | ||
passcode := os.Getenv("VORTEXNOTES_PASSCODE") | ||
auth := "none" | ||
|
||
if needAuthScopes == "" { | ||
needAuthScopes = "show,create,edit,delete" | ||
} | ||
|
||
if passcode != "" { | ||
auth = "passcode" | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"auth_scope": needAuthScopes, | ||
"auth": auth, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package middlewares | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func HasPermission(scope string) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
expectedPasscode := os.Getenv("VORTEXNOTES_PASSCODE") | ||
authorizationHeader := c.GetHeader("Authorization") | ||
passcode := strings.TrimPrefix(authorizationHeader, "Bearer ") | ||
|
||
needAuthScopes := os.Getenv("VORTEXNOTES_AUTH_SCOPE") | ||
if needAuthScopes == "" { | ||
needAuthScopes = "show,create,edit,delete" | ||
} | ||
|
||
if expectedPasscode == "" { | ||
c.Next() | ||
return | ||
} | ||
|
||
if strings.Contains(needAuthScopes, scope) { | ||
if passcode != expectedPasscode { | ||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"message": "Unauthorized"}) | ||
return | ||
} | ||
} | ||
|
||
c.Next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,30 @@ | ||
import axios from 'axios' | ||
|
||
const http = axios.create({ | ||
baseURL: location.origin.replace('7702', '7701') + '/api/' | ||
}) | ||
export const getAxiosInstance = () => { | ||
const instance = axios.create({ | ||
baseURL: location.origin.replace('7702', '7701') + '/api/', | ||
headers: { | ||
Authorization: localStorage.vortexnotes_passcode | ||
? 'Bearer ' + localStorage.vortexnotes_passcode | ||
: undefined | ||
} | ||
}) | ||
|
||
window.$http = http | ||
instance.interceptors.response.use( | ||
response => response, | ||
error => { | ||
if (error.response) { | ||
if (error.response.status === 401) { | ||
localStorage.removeItem('vortexnotes_passcode') | ||
localStorage.removeItem('vortexnotes_auth_scope') | ||
location.href = '/auth' | ||
} | ||
} | ||
return Promise.reject(error) | ||
} | ||
) | ||
|
||
return instance | ||
} | ||
|
||
window.$http = getAxiosInstance() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useEffect } from 'react' | ||
import { hasPermission } from '@/utils' | ||
import { useNavigate } from 'react-router-dom' | ||
|
||
export default function usePermissionCheckEffect(scope: string) { | ||
const navigate = useNavigate() | ||
|
||
useEffect(() => { | ||
if (!hasPermission(scope)) { | ||
navigate('/auth') | ||
} | ||
}, [navigate, scope]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.