Skip to content

Commit

Permalink
fix(auth): permission issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kangfenmao committed Dec 5, 2023
1 parent 1869b83 commit f362db7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 32 deletions.
8 changes: 5 additions & 3 deletions backend/api/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ func Auth(c *gin.Context) {
return
}

if requestData.Passcode != passcode {
c.JSON(http.StatusBadRequest, gin.H{"message": "ePasscode Invalid"})
return
if passcode != "" {
if requestData.Passcode != passcode {
c.JSON(http.StatusBadRequest, gin.H{"message": "Passcode Invalid"})
return
}
}

authScopes := os.Getenv("VORTEXNOTES_AUTH_SCOPE")
Expand Down
12 changes: 8 additions & 4 deletions backend/api/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ import (
func Config(c *gin.Context) {
needAuthScopes := os.Getenv("VORTEXNOTES_AUTH_SCOPE")
passcode := os.Getenv("VORTEXNOTES_PASSCODE")
auth := "none"
authType := "none"

if needAuthScopes == "" {
needAuthScopes = "show,create,edit,delete"
}

if passcode != "" {
auth = "passcode"
authType = "passcode"
c.JSON(http.StatusOK, gin.H{
"auth_type": authType,
"auth_scope": needAuthScopes,
})
return
}

c.JSON(http.StatusOK, gin.H{
"auth_scope": needAuthScopes,
"auth": auth,
"auth_type": authType,
})
}
46 changes: 24 additions & 22 deletions frontend/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const Navbar: React.FC<Props> = () => {
const [theme, setTheme] = useTheme()

const onSearch = search.bind(this, input, navigate)
const isPasscodeAuth = localStorage.vortexnotes_auth_type === 'passcode'

useEffect(() => {
setInput(keywords)
Expand Down Expand Up @@ -100,9 +101,6 @@ const Navbar: React.FC<Props> = () => {
<Link to="/new">
<button className="flex flex-row items-center ml-5 opacity-60 hover:opacity-80 transition-opacity">
<i className="iconfont icon-add-circle text-black dark:text-white text-2xl"></i>
<span className="text-black dark:text-white ml-1" style={{ marginTop: '-2px' }}>
New Note
</span>
</button>
</Link>
)}
Expand All @@ -124,25 +122,29 @@ const Navbar: React.FC<Props> = () => {
</div>
</Link>
</li>
{!hasPasscode() && (
<li>
<Link to="/auth">
<div className="flex flex-row items-center opacity-60">
<i className="iconfont icon-user text-black dark:text-white text-2xl"></i>
<span className="text-black dark:text-white ml-2">Login</span>
</div>
</Link>
</li>
)}
{hasPasscode() && (
<li>
<Link to="" onClick={onLogout}>
<div className="flex flex-row items-center opacity-60">
<i className="iconfont icon-logout text-black dark:text-white text-2xl"></i>
<span className="text-black dark:text-white ml-2">Logout</span>
</div>
</Link>
</li>
{isPasscodeAuth && (
<>
{!hasPasscode() && (
<li>
<Link to="/auth">
<div className="flex flex-row items-center opacity-60">
<i className="iconfont icon-user text-black dark:text-white text-2xl"></i>
<span className="text-black dark:text-white ml-2">Login</span>
</div>
</Link>
</li>
)}
{hasPasscode() && (
<li>
<Link to="" onClick={onLogout}>
<div className="flex flex-row items-center opacity-60">
<i className="iconfont icon-logout text-black dark:text-white text-2xl"></i>
<span className="text-black dark:text-white ml-2">Logout</span>
</div>
</Link>
</li>
)}
</>
)}
</ul>
</div>
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/utils/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export async function fetchConfig() {
const res = await window.$http.get('config')
const authScopes = res.data?.auth_scope
const authType = res.data?.auth_type

if (authType) {
localStorage.vortexnotes_auth_type = authType
}

if (authScopes) {
localStorage.vortexnotes_auth_scope = authScopes
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ export function onSearch(keywords: string, navigate: NavigateFunction) {
}

export const hasPermission = (scope: string) => {
if (!localStorage.vortexnotes_passcode) {
if (localStorage.vortexnotes_auth_type === 'passcode') {
if (localStorage.vortexnotes_auth_scope?.includes(scope)) {
return false
if (!localStorage.vortexnotes_passcode) {
return false
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion vortexnotes.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<module name="vortexnotes" />
<working_directory value="$PROJECT_DIR$/app" />
<envs>
<env name="VORTEXNOTES_AUTH_SCOPE" value="create,edit,delete" />
<env name="VORTEXNOTES_AUTH_SCOPE" value="show,create,edit,delete" />
<env name="VORTEXNOTES_PASSCODE" value="123456" />
</envs>
<kind value="PACKAGE" />
Expand Down

0 comments on commit f362db7

Please sign in to comment.