This commit is contained in:
Godopu 2022-12-26 14:48:30 +09:00
parent 24de27a714
commit 5864585b61
3 changed files with 68 additions and 5 deletions

50
webserver/authmod/jwt.go Normal file
View File

@ -0,0 +1,50 @@
package authmod
import (
"time"
"github.com/coreos/go-oidc"
"github.com/golang-jwt/jwt/v4"
"github.com/google/uuid"
)
type AuthTokenClaims struct {
jwt.RegisteredClaims // 표준 토큰 Claims
UserInfo *oidc.UserInfo
}
var TknHmacSecret []byte = nil
func init() {
TknHmacSecret = []byte(uuid.New().String())
}
func IssueJWT(userInfo *oidc.UserInfo, period time.Duration) (string, error) {
claims := AuthTokenClaims{
UserInfo: userInfo,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(period)),
IssuedAt: jwt.NewNumericDate(time.Now()),
NotBefore: jwt.NewNumericDate(time.Now()),
},
}
tkn := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
ss, err := tkn.SignedString([]byte(TknHmacSecret))
return ss, err
}
func ParseJWTwithClaims(ss string) (*AuthTokenClaims, error) {
claims := AuthTokenClaims{}
_, err := jwt.ParseWithClaims(ss, &claims, func(token *jwt.Token) (interface{}, error) {
return TknHmacSecret, nil
})
if err != nil {
return nil, err
}
return &claims, nil
}

View File

@ -4,7 +4,9 @@ import (
"context" "context"
"crypto/rand" "crypto/rand"
"encoding/base64" "encoding/base64"
"fmt"
"io" "io"
"iothomepage/authmod"
"log" "log"
"net/http" "net/http"
"time" "time"
@ -93,11 +95,17 @@ func authCallback(ctx *gin.Context) {
return return
} }
_ = userInfo ss, err := authmod.IssueJWT(userInfo, time.Second)
if err != nil {
http.Error(ctx.Writer, "generate jwt error", http.StatusInternalServerError)
return
}
fmt.Println(userInfo)
c := &http.Cookie{ c := &http.Cookie{
Name: "__edit_access_token_", Name: "__edit_access_token_",
Value: "temporary-token", Value: ss,
MaxAge: int(time.Hour.Seconds()), MaxAge: int(time.Hour.Seconds()),
Secure: ctx.Request.TLS != nil, Secure: ctx.Request.TLS != nil,
HttpOnly: true, HttpOnly: true,

View File

@ -2,6 +2,7 @@ package router
import ( import (
"fmt" "fmt"
"iothomepage/authmod"
"log" "log"
"net/http" "net/http"
"strings" "strings"
@ -28,7 +29,7 @@ func NewRouter() *gin.Engine {
r.Any("/*any", func(c *gin.Context) { r.Any("/*any", func(c *gin.Context) {
defer handleError(c) defer handleError(c)
path := c.Param("any") path := c.Param("any")
if strings.HasPrefix(path, "/home") { if strings.HasPrefix(path, "/dashboard") {
assetEngine.HandleContext(c) assetEngine.HandleContext(c)
return return
} else if strings.HasPrefix(path, "/auth") { } else if strings.HasPrefix(path, "/auth") {
@ -70,8 +71,12 @@ func checkAuthority(c *gin.Context) bool {
return false return false
} }
// editAuth check claims, err := authmod.ParseJWTwithClaims(editAuth)
_ = editAuth if err != nil {
return false
}
fmt.Println(claims.UserInfo)
return true return true
} }