Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
bitmask-vpn
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
Vladimir KozLove
bitmask-vpn
Commits
b9cae0b7
Unverified
Commit
b9cae0b7
authored
4 years ago
by
Kali Kaneko
Browse files
Options
Downloads
Patches
Plain Diff
[feat] cache authentication token
parent
a10c5ecd
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
pkg/config/unix.go
+2
-1
2 additions, 1 deletion
pkg/config/unix.go
pkg/vpn/bonafide/auth_sip.go
+54
-5
54 additions, 5 deletions
pkg/vpn/bonafide/auth_sip.go
pkg/vpn/bonafide/bonafide.go
+18
-4
18 additions, 4 deletions
pkg/vpn/bonafide/bonafide.go
pkg/vpn/openvpn.go
+1
-8
1 addition, 8 deletions
pkg/vpn/openvpn.go
with
75 additions
and
18 deletions
pkg/config/unix.go
+
2
−
1
View file @
b9cae0b7
...
...
@@ -18,7 +18,8 @@ package config
import
(
"os"
"path"
)
// Path for the config files
var
Path
=
os
.
Getenv
(
"HOME"
)
+
"
/
.config
/
leap"
var
Path
=
path
.
Join
(
os
.
Getenv
(
"HOME"
)
,
".config
"
,
"
leap"
)
This diff is collapsed.
Click to expand it.
pkg/vpn/bonafide/auth_sip.go
+
54
−
5
View file @
b9cae0b7
...
...
@@ -19,7 +19,13 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"time"
"0xacab.org/leap/bitmask-vpn/pkg/config"
)
type
sipAuthentication
struct
{
...
...
@@ -32,10 +38,11 @@ func (a *sipAuthentication) needsCredentials() bool {
}
func
(
a
*
sipAuthentication
)
getToken
(
user
,
password
string
)
([]
byte
,
error
)
{
/* TODO
[ ] get token from disk?
[ ] check if expired? set a goroutine to refresh it periodically?
*/
/* TODO refresh session token periodically */
if
hasRecentToken
()
{
log
.
Println
(
"Got cached token"
)
return
readToken
()
}
credJSON
,
err
:=
formatCredentials
(
user
,
password
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Cannot encode credentials: %s"
,
err
)
...
...
@@ -48,7 +55,49 @@ func (a *sipAuthentication) getToken(user, password string) ([]byte, error) {
if
resp
.
StatusCode
!=
200
{
return
nil
,
fmt
.
Errorf
(
"Cannot get token: Error %d"
,
resp
.
StatusCode
)
}
return
ioutil
.
ReadAll
(
resp
.
Body
)
token
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
return
nil
,
err
}
writeToken
(
token
)
return
token
,
nil
}
func
getTokenPath
()
string
{
return
path
.
Join
(
config
.
Path
,
config
.
ApplicationName
+
".token"
)
}
func
writeToken
(
token
[]
byte
)
{
tp
:=
getTokenPath
()
err
:=
ioutil
.
WriteFile
(
tp
,
token
,
0600
)
if
err
!=
nil
{
log
.
Println
(
"BUG: cannot write token to"
,
tp
)
}
}
func
readToken
()
([]
byte
,
error
)
{
f
,
err
:=
os
.
Open
(
getTokenPath
())
if
err
!=
nil
{
log
.
Println
(
"Error: cannot open token file"
)
return
nil
,
err
}
token
,
err
:=
ioutil
.
ReadAll
(
f
)
if
err
!=
nil
{
log
.
Println
(
"Error: cannot read token"
)
return
nil
,
err
}
return
token
,
nil
}
func
hasRecentToken
()
bool
{
statinfo
,
err
:=
os
.
Stat
(
getTokenPath
())
if
err
!=
nil
{
return
false
}
lastWrote
:=
statinfo
.
ModTime
()
.
Unix
()
/* in vpnweb we set the duration of the token to 24 hours */
old
:=
time
.
Now
()
.
Add
(
-
time
.
Hour
*
20
)
.
Unix
()
return
lastWrote
>=
old
}
func
formatCredentials
(
user
,
pass
string
)
(
string
,
error
)
{
...
...
This diff is collapsed.
Click to expand it.
pkg/vpn/bonafide/bonafide.go
+
18
−
4
View file @
b9cae0b7
...
...
@@ -114,8 +114,18 @@ func New() *Bonafide {
return
b
}
/* NeedsCredentials signals if we have to ask user for credentials. If false, it can be that we have a cached token */
func
(
b
*
Bonafide
)
NeedsCredentials
()
bool
{
return
b
.
auth
.
needsCredentials
()
if
!
b
.
auth
.
needsCredentials
()
{
return
false
}
/* try cached */
/* TODO cleanup this call - maybe expose getCachedToken instead of relying on empty creds? */
_
,
err
:=
b
.
auth
.
getToken
(
""
,
""
)
if
err
!=
nil
{
return
true
}
return
false
}
func
(
b
*
Bonafide
)
DoLogin
(
username
,
password
string
)
(
bool
,
error
)
{
...
...
@@ -136,9 +146,13 @@ func (b *Bonafide) GetPemCertificate() ([]byte, error) {
if
b
.
auth
==
nil
{
log
.
Fatal
(
"ERROR: bonafide did not initialize auth"
)
}
if
b
.
auth
.
needsCredentials
()
&&
b
.
token
==
nil
{
log
.
Println
(
"Needs token, but token is empty"
)
return
nil
,
errors
.
New
(
"Needs to login, but it was not logged in. Please, restart the application and report it if it continues happening"
)
if
b
.
auth
.
needsCredentials
()
{
/* try cached token */
token
,
err
:=
b
.
auth
.
getToken
(
""
,
""
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"BUG: This service needs login, but we were not logged in."
)
}
b
.
token
=
token
}
req
,
err
:=
http
.
NewRequest
(
"POST"
,
b
.
getURL
(
"certv3"
),
strings
.
NewReader
(
""
))
...
...
This diff is collapsed.
Click to expand it.
pkg/vpn/openvpn.go
+
1
−
8
View file @
b9cae0b7
...
...
@@ -51,14 +51,7 @@ func (b *Bitmask) StartVPN(provider string) error {
}
func
(
b
*
Bitmask
)
CanStartVPN
()
bool
{
if
!
b
.
bonafide
.
NeedsCredentials
()
{
return
true
}
_
,
err
:=
b
.
getCert
()
if
err
!=
nil
{
return
false
}
return
true
return
!
b
.
bonafide
.
NeedsCredentials
()
}
func
(
b
*
Bitmask
)
startTransport
()
(
proxy
string
,
err
error
)
{
...
...
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