Fix JWT token bugs.

This commit is contained in:
Yang Luo
2021-03-14 23:08:08 +08:00
parent f014554415
commit df948e9e34
6 changed files with 39 additions and 8 deletions

View File

@@ -34,7 +34,7 @@ type Token struct {
Application string `xorm:"varchar(100)" json:"application"` Application string `xorm:"varchar(100)" json:"application"`
Code string `xorm:"varchar(100)" json:"code"` Code string `xorm:"varchar(100)" json:"code"`
AccessToken string `xorm:"varchar(100)" json:"accessToken"` AccessToken string `xorm:"mediumtext" json:"accessToken"`
ExpiresIn int `json:"expiresIn"` ExpiresIn int `json:"expiresIn"`
Scope string `xorm:"varchar(100)" json:"scope"` Scope string `xorm:"varchar(100)" json:"scope"`
TokenType string `xorm:"varchar(100)" json:"tokenType"` TokenType string `xorm:"varchar(100)" json:"tokenType"`

View File

@@ -20,12 +20,14 @@ import (
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
) )
var jwtSecret = []byte("aaa") var jwtSecret = []byte("CasdoorSecret")
type Claims struct { type Claims struct {
Username string `json:"username"` Organization string `json:"organization"`
Name string `json:"name"` Username string `json:"username"`
Email string `json:"email"` Name string `json:"name"`
Email string `json:"email"`
IsAdmin bool `json:"isAdmin"`
jwt.StandardClaims jwt.StandardClaims
} }
@@ -34,9 +36,11 @@ func generateJwtToken(application *Application, user *User) (string, error) {
expireTime := nowTime.Add(time.Duration(application.ExpireInHours) * time.Hour) expireTime := nowTime.Add(time.Duration(application.ExpireInHours) * time.Hour)
claims := Claims{ claims := Claims{
Username: user.Name, Organization: user.Owner,
Name: user.DisplayName, Username: user.Name,
Email: user.Email, Name: user.DisplayName,
Email: user.Email,
IsAdmin: user.IsAdmin,
StandardClaims: jwt.StandardClaims{ StandardClaims: jwt.StandardClaims{
Audience: application.ClientId, Audience: application.ClientId,
ExpiresAt: expireTime.Unix(), ExpiresAt: expireTime.Unix(),

View File

@@ -8,6 +8,7 @@
"@testing-library/react": "^9.3.2", "@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2", "@testing-library/user-event": "^7.1.2",
"antd": "^4.7.2", "antd": "^4.7.2",
"copy-to-clipboard": "^3.3.1",
"i18next": "^19.8.9", "i18next": "^19.8.9",
"moment": "^2.29.1", "moment": "^2.29.1",
"react": "^16.14.0", "react": "^16.14.0",

View File

@@ -17,6 +17,7 @@ import React from "react";
import {isMobile as isMobileDevice} from "react-device-detect"; import {isMobile as isMobileDevice} from "react-device-detect";
import "./i18n"; import "./i18n";
import i18next from "i18next"; import i18next from "i18next";
import copy from "copy-to-clipboard";
export let ServerUrl = ""; export let ServerUrl = "";
@@ -151,3 +152,14 @@ export function changeLanguage(language) {
i18next.changeLanguage(language) i18next.changeLanguage(language)
window.location.reload(true); window.location.reload(true);
} }
export function getClickable(text) {
return (
<a onClick={() => {
copy(text);
showMessage("success", `Copied to clipboard`);
}}>
{text}
</a>
)
}

View File

@@ -130,6 +130,9 @@ class TokenListPage extends React.Component {
key: 'code', key: 'code',
// width: '150px', // width: '150px',
sorter: (a, b) => a.code.localeCompare(b.code), sorter: (a, b) => a.code.localeCompare(b.code),
render: (text, record, index) => {
return Setting.getClickable(text);
}
}, },
{ {
title: i18next.t("token:Access Token"), title: i18next.t("token:Access Token"),
@@ -137,6 +140,10 @@ class TokenListPage extends React.Component {
key: 'accessToken', key: 'accessToken',
// width: '150px', // width: '150px',
sorter: (a, b) => a.accessToken.localeCompare(b.accessToken), sorter: (a, b) => a.accessToken.localeCompare(b.accessToken),
ellipsis: true,
render: (text, record, index) => {
return Setting.getClickable(text);
}
}, },
{ {
title: i18next.t("token:Expires In"), title: i18next.t("token:Expires In"),

View File

@@ -3387,6 +3387,13 @@ copy-to-clipboard@^3.2.0:
dependencies: dependencies:
toggle-selection "^1.0.6" toggle-selection "^1.0.6"
copy-to-clipboard@^3.3.1:
version "3.3.1"
resolved "https://registry.npmjs.org/copy-to-clipboard/-/copy-to-clipboard-3.3.1.tgz#115aa1a9998ffab6196f93076ad6da3b913662ae"
integrity sha512-i13qo6kIHTTpCm8/Wup+0b1mVWETvu2kIMzKoK8FpkLkFxlt0znUAHcMzox+T8sPlqtZXq3CulEjQHsYiGFJUw==
dependencies:
toggle-selection "^1.0.6"
core-js-compat@^3.6.2: core-js-compat@^3.6.2:
version "3.6.5" version "3.6.5"
resolved "https://registry.npm.taobao.org/core-js-compat/download/core-js-compat-3.6.5.tgz#2a51d9a4e25dfd6e690251aa81f99e3c05481f1c" resolved "https://registry.npm.taobao.org/core-js-compat/download/core-js-compat-3.6.5.tgz#2a51d9a4e25dfd6e690251aa81f99e3c05481f1c"