Пример #1
0
func presentQRCode(pl meddler.Payload, res http.ResponseWriter, req *http.Request) {
	foundKeySet, err := lookUpKeySet(pl.PublicKey)

	if err != nil {
		http.Error(res, fmt.Sprintf("encountered error: %v", err), 400)
		return
	}

	if foundKeySet == nil {
		panic("null keySet returned from publicKeyLookup")
	}

	rawTextForSigning := pl.RawTextForSigning()
	if !foundKeySet.Match([]byte(rawTextForSigning), []byte(pl.Signature)) {
		http.Error(res, "invalid signature", 403)
		return
	}

	curTimeUnix := time.Now().Unix()
	if pl.ExpiryTime < curTimeUnix {
		http.Error(res, fmt.Sprintf("request expired at %q, current time %q", pl.ExpiryTime, curTimeUnix), 403)
		return
	}

	uri := pl.URI
	code, err := qr.Encode(uri, qr.Q)
	if err != nil {
		fmt.Fprintf(res, "%s %v\n", uri, err)
		return
	}

	pngImage := code.PNG()
	fmt.Fprintf(res, "%s", pngImage)
}
Пример #2
0
func presentQRCode(pl meddler.Payload, res http.ResponseWriter, req *http.Request) {
	if pl.PublicKey != envKeySet.PublicKey {
		http.Error(res, "invalid publickey", 405)
		return
	}

	rawTextForSigning := pl.RawTextForSigning()
	if !envKeySet.Match([]byte(rawTextForSigning), []byte(pl.Signature)) {
		http.Error(res, "invalid signature", 403)
		return
	}

	curTimeUnix := time.Now().Unix()
	if pl.ExpiryTime < curTimeUnix {
		http.Error(res, fmt.Sprintf("request expired at %q, current time %q", pl.ExpiryTime, curTimeUnix), 403)
		return
	}

	uri := pl.URI
	code, err := qr.Encode(uri, qr.Q)
	if err != nil {
		fmt.Fprintf(res, "%s %v\n", uri, err)
		return
	}

	pngImage := code.PNG()
	fmt.Fprintf(res, "%s", pngImage)
}
Пример #3
0
func (g *Commands) QR(byId bool) error {

	kvChan := g.urler(byId, g.opts.Sources)

	address := DefaultQRShareServer
	if g.opts.Meta != nil {
		meta := *(g.opts.Meta)
		if retrAddress, ok := meta[AddressKey]; ok && len(retrAddress) >= 1 {
			address = retrAddress[0]
		}
	}

	address = strings.TrimRight(address, "/")
	if envKeySet.PublicKey == "" {
		envKeySet.PublicKey = "5160897b3586461e83e7279c10352ac4"
	}
	if envKeySet.PrivateKey == "" {
		envKeySet.PrivateKey = "5a3451dadab74f75b16f754c0a931949"
	}

	for kv := range kvChan {
		switch kv.value.(type) {
		case error:
			g.log.LogErrf("%s: %s\n", kv.key, kv.value)
			continue
		}

		fileURI, ok := kv.value.(string)
		if !ok {
			continue
		}

		curTime := time.Now()
		pl := meddler.Payload{
			URI:         fileURI,
			RequestTime: curTime.Unix(),
			Payload:     fmt.Sprintf("%v%v", rand.Float64(), curTime),
			PublicKey:   envKeySet.PublicKey,
			ExpiryTime:  curTime.Add(time.Hour).Unix(),
		}

		plainTextToSign := pl.RawTextForSigning()

		pl.Signature = fmt.Sprintf("%s", envKeySet.Sign([]byte(plainTextToSign)))

		uv := pl.ToUrlValues()
		encodedValues := uv.Encode()

		fullUrl := fmt.Sprintf("%s/qr?%s", address, encodedValues)
		if g.opts.Verbose {
			g.log.Logf("%q => %q\n", kv.key, fullUrl)
		}

		if err := open.Start(fullUrl); err != nil {
			g.log.Logf("qr: err %v %q\n", err, fullUrl)
		}
	}

	return nil
}