Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
cicer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
meskio
cicer
Commits
0480c4a9
Commit
0480c4a9
authored
4 years ago
by
meskio
Browse files
Options
Downloads
Patches
Plain Diff
Create tokens that don't expire
parent
c3f1204f
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
api/api.go
+1
-1
1 addition, 1 deletion
api/api.go
api/auth.go
+16
-7
16 additions, 7 deletions
api/auth.go
src/App.js
+10
-7
10 additions, 7 deletions
src/App.js
src/SignIn.js
+15
-1
15 additions, 1 deletion
src/SignIn.js
with
42 additions
and
16 deletions
api/api.go
+
1
−
1
View file @
0480c4a9
...
...
@@ -31,7 +31,7 @@ func Init(dbPath string, signKey string, r *mux.Router) error {
a
:=
api
{
db
,
[]
byte
(
signKey
)}
token
,
err
:=
a
.
newToken
(
0
,
"admin"
)
token
,
err
:=
a
.
newToken
(
0
,
"admin"
,
false
)
log
.
Print
(
token
)
r
.
HandleFunc
(
"/signin"
,
a
.
SignIn
)
.
Methods
(
"POST"
)
...
...
This diff is collapsed.
Click to expand it.
api/auth.go
+
16
−
7
View file @
0480c4a9
...
...
@@ -15,6 +15,7 @@ import (
type
creds
struct
{
Name
string
`json:"name"`
Password
string
`json:"password"`
NoExpire
bool
`json:"noExpire"`
}
func
(
a
*
api
)
SignIn
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
...
...
@@ -44,7 +45,7 @@ func (a *api) SignIn(w http.ResponseWriter, req *http.Request) {
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
w
.
WriteHeader
(
http
.
StatusOK
)
token
,
err
:=
a
.
newToken
(
member
.
Num
,
member
.
Role
)
token
,
err
:=
a
.
newToken
(
member
.
Num
,
member
.
Role
,
!
c
.
NoExpire
)
if
err
!=
nil
{
log
.
Printf
(
"Can't create a token: %v"
,
err
)
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
...
...
@@ -80,7 +81,7 @@ func (a *api) GetToken(w http.ResponseWriter, req *http.Request) {
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
w
.
WriteHeader
(
http
.
StatusOK
)
token
,
err
:=
a
.
newToken
(
int
(
num
),
role
)
token
,
err
:=
a
.
newToken
(
int
(
num
),
role
,
true
)
if
err
!=
nil
{
log
.
Printf
(
"Can't create a token: %v"
,
err
)
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
...
...
@@ -162,12 +163,16 @@ func (a *api) authNumRole(fn func(int, string, http.ResponseWriter, *http.Reques
}
}
func
(
a
*
api
)
newToken
(
num
int
,
role
string
)
(
string
,
error
)
{
token
:=
jwt
.
NewWithClaims
(
jwt
.
SigningMethodHS256
,
jwt
.
MapClaims
{
func
(
a
*
api
)
newToken
(
num
int
,
role
string
,
expire
bool
)
(
string
,
error
)
{
claims
:=
jwt
.
MapClaims
{
"num"
:
num
,
"role"
:
role
,
"exp"
:
time
.
Now
()
.
Add
(
time
.
Hour
*
24
)
.
Unix
(),
})
}
if
expire
{
claims
[
"exp"
]
=
time
.
Now
()
.
Add
(
time
.
Hour
*
24
)
.
Unix
()
}
token
:=
jwt
.
NewWithClaims
(
jwt
.
SigningMethodHS256
,
claims
)
return
token
.
SignedString
(
a
.
signKey
)
}
...
...
@@ -185,7 +190,11 @@ func (a *api) validateToken(token string) (bool, jwt.MapClaims) {
if
!
ok
{
return
false
,
nil
}
exp
,
ok
:=
claims
[
"exp"
]
.
(
float64
)
expClaim
,
ok
:=
claims
[
"exp"
]
if
!
ok
{
return
true
,
claims
}
exp
,
ok
:=
expClaim
.
(
float64
)
if
!
ok
{
return
false
,
claims
}
...
...
This diff is collapsed.
Click to expand it.
src/App.js
+
10
−
7
View file @
0480c4a9
...
...
@@ -73,10 +73,6 @@ class App extends React.Component {
this
.
storeState
(
token
,
member
.
num
,
member
.
role
);
}
isLoged
()
{
return
this
.
state
.
token
!==
null
&&
!
this
.
tokenExpired
();
}
logout
()
{
this
.
setState
({
num
:
null
,
...
...
@@ -86,16 +82,23 @@ class App extends React.Component {
this
.
storeState
(
""
,
""
,
""
);
}
tokenExpired
()
{
isLoged
()
{
if
(
!
this
.
state
.
token
)
{
return
false
;
}
const
token
=
this
.
state
.
token
;
const
base64Url
=
token
.
split
(
'
.
'
)[
1
];
const
base64
=
base64Url
.
replace
(
/-/g
,
'
+
'
).
replace
(
/_/g
,
'
/
'
);
const
jsonPayload
=
decodeURIComponent
(
atob
(
base64
).
split
(
''
).
map
(
function
(
c
)
{
return
'
%
'
+
(
'
00
'
+
c
.
charCodeAt
(
0
).
toString
(
16
)).
slice
(
-
2
);
}).
join
(
''
));
const
claims
=
JSON
.
parse
(
jsonPayload
);
return
claims
[
"
exp
"
]
<
Date
.
now
();
const
claims
=
JSON
.
parse
(
jsonPayload
);
if
(
claims
[
"
exp
"
]
===
undefined
)
{
return
true
;
}
return
claims
[
"
exp
"
]
<
Date
.
now
()
/
1000
;
};
render
()
{
...
...
This diff is collapsed.
Click to expand it.
src/SignIn.js
+
15
−
1
View file @
0480c4a9
...
...
@@ -8,6 +8,7 @@ class SignIn extends React.Component {
this
.
state
=
{
name
:
""
,
password
:
""
,
noExpire
:
false
,
isLoading
:
false
,
badAuth
:
false
,
error
:
null
...
...
@@ -21,7 +22,10 @@ class SignIn extends React.Component {
this
.
setState
({
isLoading
:
true
,
error
:
null
});
const
body
=
JSON
.
stringify
({
name
:
this
.
state
.
name
,
password
:
this
.
state
.
password
});
name
:
this
.
state
.
name
,
password
:
this
.
state
.
password
,
noExpire
:
this
.
state
.
noExpire
});
fetch
(
"
/api/signin
"
,
{
method
:
"
POST
"
,
body
})
.
then
(
response
=>
{
...
...
@@ -73,6 +77,16 @@ class SignIn extends React.Component {
Nombre
o
contraseña
invalidos
.
<
/Form.Control.Feedback
>
<
/Form.Group
>
<
Form
.
Group
>
<
Form
.
Check
type
=
"
switch
"
id
=
"
noExpire
"
label
=
"
Permanecer conectada con este navegador
"
onChange
=
{
e
=>
this
.
setState
({
noExpire
:
e
.
target
.
checked
})
}
/
>
<
/Form.Group
>
<
Button
variant
=
"
primary
"
type
=
"
submit
"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment