Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: gen staff token #32

Merged
merged 3 commits into from
Jan 11, 2024
Merged

feat: gen staff token #32

merged 3 commits into from
Jan 11, 2024

Conversation

AKKatung159
Copy link
Member

No description provided.

Copy link

linear bot commented Jan 10, 2024

OPH24-11 Staff Login

Copy link
Member

@ImSoZRious ImSoZRious left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

internal/middleware/auth.middleware.go Outdated Show resolved Hide resolved
Comment on lines 1 to 44
package main

import (
"fmt"

"github.com/dgrijalva/jwt-go"
"github.com/isd-sgcu/oph66-backend/apperror"
"github.com/isd-sgcu/oph66-backend/cfgldr"
)

func GenTokenStaff(department string, cfg *cfgldr.Config) (string, *apperror.AppError) {
secretKey := cfg.JWTConfig.SecretKey

claims := jwt.MapClaims{
"role": "staff",
"department": department,
}

token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

tokenString, err := token.SignedString([]byte(secretKey))
if err != nil {
return "", apperror.InternalError
}

return tokenString, nil
}

func main() {
config, err := cfgldr.LoadConfig()
if err != nil {
fmt.Println("Failed to load configuration:", err)
return
}

department := "IT"
token, appErr := GenTokenStaff(department, config)
if appErr != nil {
fmt.Println("Failed to generate JWT token:", appErr)
return
}

fmt.Println("Generated JWT token for staff in department", department, ":", token)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to make is bash script or something else. Making new main package will likely interrupt with ./... syntax which is used across the project.

return
}

department := "IT"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accept input from other source i.e. stdin, argv.

department := "IT"
token, appErr := GenTokenStaff(department, config)
if appErr != nil {
fmt.Println("Failed to generate JWT token:", appErr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fmt.Println("Failed to generate JWT token:", appErr)
fmt.FatalF("Failed to generate JWT token:", appErr) // something I don't remember

func main() {
config, err := cfgldr.LoadConfig()
if err != nil {
fmt.Println("Failed to load configuration:", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here


claims := jwt.MapClaims{
"role": "staff",
"department": department,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to store to match database format
so it would be
department and faculty. identify using code.

e.g.
department: 0
faculty: 34

@ImSoZRious
Copy link
Member

เรื่องคนะยังไม่แก้เลยนะ ที่ต้องเกบทั้งคนะและภาค

@ImSoZRious
Copy link
Member

ขอ request เพิ่มอีก field คือ faculty-wide เป้น boolean (เปน token คนะที่มี access ทุกภาค)

Copy link
Member

@ImSoZRious ImSoZRious left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm.

@ImSoZRious ImSoZRious merged commit ec4199e into beta Jan 11, 2024
1 check passed
@ImSoZRious ImSoZRious deleted the akkatung/oph24-11-staff-login branch January 11, 2024 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants