Browse Source

implemented complete login example (frontend and backend)

master
arnaucode 6 years ago
parent
commit
3b65da470d
24 changed files with 427 additions and 68 deletions
  1. +7
    -2
      clientApp/GUI/views/id/id.html
  2. +1
    -1
      clientApp/GUI/views/main/main.html
  3. +10
    -1
      clientApp/clientApp.go
  4. +1
    -0
      clientApp/keys.go
  5. +1
    -1
      clientApp/main.go
  6. +0
    -1
      darkID-library-login-example/.gitignore
  7. +67
    -27
      darkID-library-login-example/RESTfunctions.go
  8. +2
    -1
      darkID-library-login-example/config.json
  9. +7
    -4
      darkID-library-login-example/main.go
  10. +2
    -1
      darkID-library-login-example/readConfig.go
  11. +6
    -6
      darkID-library-login-example/restRoutes.go
  12. +0
    -20
      darkID-library-login-example/testUser.sh
  13. +3
    -0
      darkID-library-login-example/web/.bowerrc
  14. +2
    -0
      darkID-library-login-example/web/.gitignore
  15. +63
    -0
      darkID-library-login-example/web/app.js
  16. +19
    -0
      darkID-library-login-example/web/bower.json
  17. +0
    -0
      darkID-library-login-example/web/css/own.css
  18. +74
    -0
      darkID-library-login-example/web/index.html
  19. +11
    -0
      darkID-library-login-example/web/package.json
  20. +43
    -0
      darkID-library-login-example/web/views/login/login.html
  21. +64
    -0
      darkID-library-login-example/web/views/login/login.js
  22. +19
    -0
      darkID-library-login-example/web/views/main/main.html
  23. +15
    -0
      darkID-library-login-example/web/views/main/main.js
  24. +10
    -3
      runTmuxTestPeers.sh

+ 7
- 2
clientApp/GUI/views/id/id.html

@ -7,6 +7,11 @@
<div class="card">
<div class="card-body">
<h4>ID: {{id.id}}</h4>
<div class="row">
<textarea disabled style="color:#81C784;width:100%;" rows="4"
ng-model="id.publicKey"
></textarea>
</div>
<span class="pull-right">{{id.date | date: "dd.MM.y, HH:mm:ss"}}h</span>
@ -23,7 +28,7 @@
</div>
<h5 ng-show="decryptData.m">PoD:</h5>
{{decryptData.m}}
<div ng-click="decrypt()" ng-show="id.blockchainref" class="btn btn-raised pull-right c_o_pink300">
<div ng-click="decrypt()" ng-show="id.unblindedsig" class="btn btn-raised pull-right c_o_pink300">
Proof of decrypt
</div>
@ -42,7 +47,7 @@
placeholder="Encrypted data..."
></textarea>
</div>
<div ng-click="encrypt()" ng-show="id.blockchainref" class="btn btn-raised pull-right c_o_orange300">
<div ng-click="encrypt()" ng-show="id.unblindedsig" class="btn btn-raised pull-right c_o_orange300">
Encrypt
</div>
</div>

+ 1
- 1
clientApp/GUI/views/main/main.html

@ -26,7 +26,7 @@
<div ng-click="clientApp('addtoblockchain', id.id)" ng-show="id.unblindedsig && !id.blockchainref" class="btn btn-raised btn-sm c_o_deepPurple300">
<i title="" class="fa fa-chain"></i> Add to blockchain
</div>
<a ng-href="#!/id/{{id.id}}" ng-show="id.blockchainref" class="btn btn-raised btn-sm c_o_green300">
<a ng-href="#!/id/{{id.id}}" ng-show="id.unblindedsig" class="btn btn-raised btn-sm c_o_green300">
Use ID
</a>
</div>

+ 10
- 1
clientApp/clientApp.go

@ -8,6 +8,7 @@ import (
"crypto/sha1"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os/exec"
"time"
@ -37,8 +38,16 @@ func NewID() []Key {
key.PrivK = id + "private.pem"
key.PubK = id + "public.pem"
time.Sleep(time.Second * 2)
b, err := ioutil.ReadFile(keysDir + "/" + key.PubK)
if err != nil {
fmt.Print(err)
}
key.PublicKey = string(b)
key.Date = time.Now()
fmt.Println(key)
fmt.Println(key.PublicKey)
keys := readKeys()
keys = append(keys, key)

+ 1
- 0
clientApp/keys.go

@ -19,6 +19,7 @@ type Key struct {
ID string `json:"id"`
PrivK string `json:"privK"` //path of the PrivK file
PubK string `json:"pubK"` //path of the PubK file
PublicKey string `json:"publicKey"`
Date time.Time `json:"date"`
Hashed []byte `json:"hashed"`
UnblindedSig []byte `json:"unblindedsig"`

+ 1
- 1
clientApp/main.go

@ -40,6 +40,6 @@ func main() {
func GUI() {
//here, run webserver
log.Println("webserver in port " + "8080")
http.Handle("/", http.FileServer(http.Dir("./web")))
http.Handle("/", http.FileServer(http.Dir("./GUI")))
http.ListenAndServe(":"+"8080", nil)
}

+ 0
- 1
darkID-library-login-example/.gitignore

@ -1,3 +1,2 @@
keys.json
keys
web

+ 67
- 27
darkID-library-login-example/RESTfunctions.go

@ -1,11 +1,16 @@
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"encoding/json"
"errors"
"fmt"
mrand "math/rand"
"net/http"
"strings"
"github.com/cryptoballot/rsablind"
"github.com/fatih/color"
"gopkg.in/mgo.v2/bson"
@ -22,54 +27,89 @@ func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "clientApp")
}
func Signup(w http.ResponseWriter, r *http.Request) {
type Proof struct {
PublicKey string `json:"publicKey"`
Clear string `json:"clear"`
Question []byte `json:"question"`
Answer string `json:"answer"`
}
var proofs []Proof
func GetProof(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var user User
err := decoder.Decode(&user)
var receivedProof Proof
err := decoder.Decode(&receivedProof)
if err != nil {
panic(err)
}
defer r.Body.Close()
//TODO check if the user password exists in the database
stringPublicKey := strings.Replace(receivedProof.PublicKey, " ", "\n", -1)
stringPublicKey = strings.Replace(stringPublicKey, "-----BEGIN\n", "-----BEGIN ", -1)
stringPublicKey = strings.Replace(stringPublicKey, "-----END\n", "-----END ", -1)
stringPublicKey = strings.Replace(stringPublicKey, "PUBLIC\n", "PUBLIC ", -1)
color.Green(stringPublicKey)
publicKey, err := ParseRsaPublicKeyFromPemStr(stringPublicKey)
check(err)
fmt.Print("user signup: ")
fmt.Println(user)
var proof Proof
proof.Clear = RandStringRunes(40)
jResp, err := json.Marshal(user)
out, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, &publicKey, []byte(proof.Clear), []byte("orders"))
check(err)
proof.Question = out
proofs = append(proofs, proof)
proof.Clear = ""
jResp, err := json.Marshal(proof)
if err != nil {
panic(err)
}
fmt.Fprintln(w, string(jResp))
}
func Login(w http.ResponseWriter, r *http.Request) {
func AnswerProof(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var key Key
err := decoder.Decode(&key)
var ansProof Proof
err := decoder.Decode(&ansProof)
if err != nil {
panic(err)
}
defer r.Body.Close()
//TODO check if the user password exists in the database
fmt.Print("key login: ")
fmt.Println(key)
token, err := newToken()
check(err)
proof, err := getProofFromStorage(ansProof.PublicKey)
if err != nil {
//validate if the pubK darkID is in the blockchain
}
if ansProof.Answer == proof.Clear {
token, err := newToken()
check(err)
fmt.Fprintln(w, string(token))
}
//verify that the darkID is signed
if err := rsablind.VerifyBlindSignature(key.ServerVerifier, key.Hashed, key.UnblindedSig); err != nil {
fmt.Println(err)
} else {
color.Green("blind signature verified")
fmt.Fprintln(w, string("fail"))
}
func getProofFromStorage(publicKey string) (Proof, error) {
var voidProof Proof
for _, proof := range proofs {
if proof.PublicKey == publicKey {
return proof, nil
}
}
return voidProof, errors.New("proof not exist in storage")
}
/*jResp, err := json.Marshal(token)
if err != nil {
panic(err)
}*/
fmt.Fprintln(w, string(token))
//function to generate random string of fixed length
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[mrand.Intn(len(letterRunes))]
}
return string(b)
}

+ 2
- 1
darkID-library-login-example/config.json

@ -1,3 +1,4 @@
{
"port": "5000"
"port": "5010",
"webserverport": "5011"
}

+ 7
- 4
darkID-library-login-example/main.go

@ -3,7 +3,9 @@ package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"time"
"github.com/fatih/color"
"github.com/gorilla/handlers"
@ -19,9 +21,10 @@ func main() {
readConfig("config.json")
fmt.Println(config)
/*//create keys directory
_ = os.Mkdir(keysDir, os.ModePerm)*/
//initialize rand
rand.Seed(time.Now().UnixNano())
//initialize token
initializeToken()
//run thw webserver
@ -40,7 +43,7 @@ func main() {
func GUI() {
//here, run webserver
log.Println("webserver in port " + "8080")
log.Println("webserver in port " + config.WebServerPort)
http.Handle("/", http.FileServer(http.Dir("./web")))
http.ListenAndServe(":"+"8080", nil)
http.ListenAndServe(":"+config.WebServerPort, nil)
}

+ 2
- 1
darkID-library-login-example/readConfig.go

@ -7,7 +7,8 @@ import (
//Config reads the config
type Config struct {
Port string `json:"port"`
Port string `json:"port"`
WebServerPort string `json:"webserverport"`
}
var config Config

+ 6
- 6
darkID-library-login-example/restRoutes.go

@ -10,15 +10,15 @@ var routes = Routes{
Index,
},
Route{
"Signup",
"GetProof",
"POST",
"/signup",
Signup,
"/getproof",
GetProof,
},
Route{
"Login",
"AnswerProof",
"POST",
"/login",
Login,
"/answerproof",
AnswerProof,
},
}

+ 0
- 20
darkID-library-login-example/testUser.sh

@ -1,20 +0,0 @@
echo ""
echo "sending the signup, response:"
curl -X POST http://127.0.0.1:3130/signup -d '{"email": "user1@e.com", "password": "user1"}'
echo ""
echo "sending the login, response:"
curl -X POST http://127.0.0.1:3130/login -d '{"email": "user1@e.com", "password": "user1"}'
echo ""
echo "send pubK and m to blind sign"
echo "json to send to the serverIDsigner:"
echo '{"pubKstring": {"e": "65537", "n": "139093"}, "m": "hola"}'
echo "serverIDsigner response:"
BLINDSIGNED=$(curl -X POST http://127.0.0.1:3130/blindsign -d '{"pubKstring": {"e": "65537", "n": "139093"}, "m": "hola"}')
echo "$BLINDSIGNED"
echo ""
echo "send blindsigned to the serverIDsigner to verify"
curl -X POST http://127.0.0.1:3130/verifysign -d '{"m": "hola", "mSigned": "131898 40373 107552 34687"}'

+ 3
- 0
darkID-library-login-example/web/.bowerrc

@ -0,0 +1,3 @@
{
"directory": "bower_components"
}

+ 2
- 0
darkID-library-login-example/web/.gitignore

@ -0,0 +1,2 @@
bower_components
node_modules

+ 63
- 0
darkID-library-login-example/web/app.js

@ -0,0 +1,63 @@
'use strict';
var urlapi = "http://127.0.0.1:5010/";
// Declare app level module which depends on views, and components
angular.module('app', [
'ngRoute',
'ngMessages',
'angularBootstrapMaterial',
'ui.bootstrap',
'toastr',
'app.main',
'app.login'
]).
config(['$locationProvider', '$routeProvider', function($locationProvider, $routeProvider) {
$locationProvider.hashPrefix('!');
$routeProvider.otherwise({
redirectTo: '/login'
});
}])
.config(function(toastrConfig) {
angular.extend(toastrConfig, {
autoDismiss: false,
containerId: 'toast-container',
maxOpened: 0,
newestOnTop: true,
positionClass: 'toast-bottom-right',
preventDuplicates: false,
preventOpenDuplicates: false,
target: 'body'
});
})
.factory('httpInterceptor', function httpInterceptor() {
return {
request: function(config) {
return config;
},
requestError: function(config) {
return config;
},
response: function(res) {
return res;
},
responseError: function(res) {
return res;
}
};
})
.factory('api', function($http) {
return {
init: function() {
/*$http.defaults.headers.common['X-Access-Token'] = localStorage.getItem('block_webapp_token');
$http.defaults.headers.post['X-Access-Token'] = localStorage.getItem('block_webapp_token');*/
}
};
})
.run(function(api) {
api.init();
});

+ 19
- 0
darkID-library-login-example/web/bower.json

@ -0,0 +1,19 @@
{
"name": "darkID-library-login-example",
"description": "",
"version": "0.0.0",
"homepage": "",
"license": "MIT",
"private": true,
"dependencies": {
"angular": "^1.6.2",
"angular-route": "^1.6.1",
"angular-messages": "^1.6.5",
"angular-bootstrap-material": "abm#^0.1.4",
"angular-bootstrap": "^2.5.0",
"components-font-awesome": "^4.7.0",
"angular-toastr": "^2.1.1",
"cssMaterialColors": "*",
"angular-chart.js": "^1.1.1"
}
}

+ 0
- 0
darkID-library-login-example/web/css/own.css


+ 74
- 0
darkID-library-login-example/web/index.html

@ -0,0 +1,74 @@
<!DOCTYPE html>
<html lang="en" ng-app="app" ng-cloak>
<head>
<meta charset="utf-8">
<title>library-login-example</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Fonts -->
<!-- in development I use googlefonts to go faster, but for the final app, I'll use downloaded fonts -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans|Overpass+Mono:700|Raleway:700" rel="stylesheet">
<link rel="stylesheet" href="bower_components/components-font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="css/own.css">
<link href="bower_components/cssMaterialColors/colors.css" rel="stylesheet">
</head>
<body ng-app="webapp">
<br><br><br><br>
<div ng-view></div>
<!-- ELECTRON
Insert this line above script imports
Works for both browser and electron with the same code -->
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>
<!-- Angular js -->
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="bower_components/angular-messages/angular-messages.js"></script>
<!-- Bootstrap -->
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
<!-- Bootstrap Material Design -->
<link rel="stylesheet" href="bower_components/bootstrap-material-design/dist/css/bootstrap-material-design.css">
<link rel="stylesheet" href="bower_components/bootstrap-material-design/dist/css/ripples.css">
<script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
<script src="bower_components/angular-bootstrap-material/dist/angular-bootstrap-material.js"></script>
<!-- jQuery for Bootstrap -->
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<!-- Angular Chart -->
<script src="bower_components/chart.js/dist/Chart.min.js"></script>
<script src="bower_components/angular-chart.js/dist/angular-chart.min.js"></script>
<!-- toastr -->
<link rel="stylesheet" type="text/css" href="bower_components/angular-toastr/dist/angular-toastr.css" />
<script type="text/javascript" src="bower_components/angular-toastr/dist/angular-toastr.tpls.js"></script>
<!-- app's js -->
<script src="app.js"></script>
<script src="views/main/main.js"></script>
<script src="views/login/login.js"></script>
<!-- ELECTRON
Insert this line after script imports -->
<script>if (window.module) module = window.module;</script>
</body>
</html>

+ 11
- 0
darkID-library-login-example/web/package.json

@ -0,0 +1,11 @@
{
"name": "darkID-library-login-example",
"version": "1.0.0",
"description": "darkID-library-login-example",
"scripts": {
"postinstall": "bower install",
"prestart": "npm install",
"start": "http-server"
},
"license": "MIT"
}

+ 43
- 0
darkID-library-login-example/web/views/login/login.html

@ -0,0 +1,43 @@
<div class="container" style="margin-top: -60px;">
<div class="row">
<div class="col-sm-4">
</div>
<div class="col-sm-4">
<div class="card">
<div class="card-body">
<h4 class="card-title">
Some platform login example
</h4>
<div class="row">
<textarea style="color:#000000;width:100%;" rows="4"
ng-model="proof.PublicKey"
placeholder="Enter here the publicKey of darkID"
></textarea>
</div>
<div class="row">
<div ng-click="getproof()" class="btn btn-block btn-raised btn-block c_indigo300 pull-right">Proof of darkID</div>
</div>
<div ng-show="proof.question">
Proof question:
<br>
<div class="row">
<textarea disabled style="color:#81C784;width:100%;" rows="4"
ng-model="proof.question"
></textarea>
</div>
<input ng-model="proof.answer" class="form-control" placeholder="Proof answer" type="text">
<div class="row">
<div ng-click="sendanswer()" class="btn btn-block btn-raised btn-block c_green300 pull-right">Send answer</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-4">
</div>
</div>
</div>

+ 64
- 0
darkID-library-login-example/web/views/login/login.js

@ -0,0 +1,64 @@
'use strict';
angular.module('app.login', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/login', {
templateUrl: 'views/login/login.html',
controller: 'LoginCtrl'
});
}])
.controller('LoginCtrl', function($scope, $rootScope, $http, $routeParams, toastr) {
$rootScope.server = ""
$scope.proof = {
publicKey: "",
clear: "",
question: "",
answer: ""
};
$scope.getproof = function() {
$http({
url: urlapi + 'getproof',
method: "POST",
headers: {
"Content-Type": undefined
},
data: $scope.proof
})
.then(function(data) {
console.log("data: ");
console.log(data.data);
$scope.proof = data.data;
},
function(data) {
console.log(data);
toastr.error("error: bad darkID PublicKey")
});
};
$scope.sendanswer = function() {
$http({
url: urlapi + 'answerproof',
method: "POST",
headers: {
"Content-Type": undefined
},
data: $scope.proof
})
.then(function(data) {
console.log("data: ");
console.log(data.data);
if(data.data=="fail\n") {
toastr.error("Proof of darkID failed");
}else{
toastr.success("You are logged with darkID!");
window.location="#!/main";
}
},
function(data) {
console.log(data);
});
};
});

+ 19
- 0
darkID-library-login-example/web/views/main/main.html

@ -0,0 +1,19 @@
<div class="container">
<div class="row">
<div class="col-sm-2">
</div>
<div class="col-sm-8">
<h4 class="card-title">
You are logged!
</h4>
<div class="card">
</div>
</div>
<div class="col-sm-2">
</div>
</div>
</div>

+ 15
- 0
darkID-library-login-example/web/views/main/main.js

@ -0,0 +1,15 @@
'use strict';
angular.module('app.main', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/main', {
templateUrl: 'views/main/main.html',
controller: 'MainCtrl'
});
}])
.controller('MainCtrl', function($scope, $rootScope, $http) {
});

+ 10
- 3
runTmuxTestPeers.sh

@ -1,11 +1,18 @@
SESSION='darkIDtest'
tmux new-session -d -s $SESSION
tmux split-window -d -t 0 -v
tmux split-window -d -t 0 -h
tmux split-window -d -t 0 -v
tmux send-keys -t 0 'cd serverIDsigner && go run *.go' enter
tmux send-keys -t 1 'cd clientApp && go run *.go' enter
tmux send-keys -t 2 'cd clientApp/GUI && http-server' enter
tmux send-keys -t 2 'cd clientApp && go run *.go' enter
tmux send-keys -t 1 'cd darkID-library-login-example && go run *.go' enter
tmux attach
# websites:
# 127.0.0.1:8080 darkID client
# 127.0.0.1:5011 library login example with darkID

Loading…
Cancel
Save