arnaucube
arnaucube/darkID-prototypePublic Self-hosted repository
darkID-prototype/darkID-library-login-example/RESTfunctions.go
darkID-library-login-example/RESTfunctions.goRaw
  1package main
  2
  3import (
  4	"crypto/rand"
  5	"crypto/rsa"
  6	"crypto/sha1"
  7	"encoding/json"
  8	"errors"
  9	"fmt"
 10	mrand "math/rand"
 11	"net/http"
 12	"strings"
 13
 14	"github.com/fatih/color"
 15
 16	"gopkg.in/mgo.v2/bson"
 17)
 18
 19type User struct {
 20	Id       bson.ObjectId `json:"id" bson:"_id,omitempty"`
 21	Email    string        `json:"email"`
 22	Password string        `json:"password"`
 23	Token    string        `json:"token"`
 24}
 25
 26func Index(w http.ResponseWriter, r *http.Request) {
 27	fmt.Fprintln(w, "clientApp")
 28}
 29
 30type Proof struct {
 31	PublicKey string `json:"publicKey"`
 32	Clear     string `json:"clear"`
 33	Question  []byte `json:"question"`
 34	Answer    string `json:"answer"`
 35}
 36
 37var proofs []Proof
 38
 39func GetProof(w http.ResponseWriter, r *http.Request) {
 40
 41	decoder := json.NewDecoder(r.Body)
 42	var receivedProof Proof
 43	err := decoder.Decode(&receivedProof)
 44	if err != nil {
 45		panic(err)
 46	}
 47	defer r.Body.Close()
 48	//TODO check if the user password exists in the database
 49
 50	stringPublicKey := strings.Replace(receivedProof.PublicKey, " ", "\n", -1)
 51	stringPublicKey = strings.Replace(stringPublicKey, "-----BEGIN\n", "-----BEGIN ", -1)
 52	stringPublicKey = strings.Replace(stringPublicKey, "-----END\n", "-----END ", -1)
 53	stringPublicKey = strings.Replace(stringPublicKey, "PUBLIC\n", "PUBLIC ", -1)
 54	color.Green(stringPublicKey)
 55	publicKey, err := ParseRsaPublicKeyFromPemStr(stringPublicKey)
 56	check(err)
 57
 58	var proof Proof
 59	proof.Clear = RandStringRunes(40)
 60
 61	out, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, &publicKey, []byte(proof.Clear), []byte("orders"))
 62	check(err)
 63	proof.Question = out
 64
 65	proofs = append(proofs, proof)
 66
 67	proof.Clear = ""
 68	jResp, err := json.Marshal(proof)
 69	if err != nil {
 70		panic(err)
 71	}
 72	fmt.Fprintln(w, string(jResp))
 73}
 74func AnswerProof(w http.ResponseWriter, r *http.Request) {
 75
 76	decoder := json.NewDecoder(r.Body)
 77	var ansProof Proof
 78	err := decoder.Decode(&ansProof)
 79	if err != nil {
 80		panic(err)
 81	}
 82	defer r.Body.Close()
 83
 84	proof, err := getProofFromStorage(ansProof.PublicKey)
 85	if err != nil {
 86
 87	}
 88	if ansProof.Answer == proof.Clear {
 89		token, err := newToken()
 90		check(err)
 91		fmt.Fprintln(w, string(token))
 92	}
 93
 94	fmt.Fprintln(w, string("fail"))
 95}
 96func getProofFromStorage(publicKey string) (Proof, error) {
 97	var voidProof Proof
 98	for _, proof := range proofs {
 99		if proof.PublicKey == publicKey {
100			return proof, nil
101		}
102	}
103	return voidProof, errors.New("proof not exist in storage")
104}
105
106//function to generate random string of fixed length
107var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
108
109func RandStringRunes(n int) string {
110	b := make([]rune, n)
111	for i := range b {
112		b[i] = letterRunes[mrand.Intn(len(letterRunes))]
113	}
114	return string(b)
115}