~kyrofa/junk/pay-service-trust-store-integration

« back to all changes in this revision

Viewing changes to service-ng/src/launchpad.net/go-trust-store/trust/request.go

  • Committer: Kyle Fazzari
  • Date: 2015-10-01 22:36:45 UTC
  • Revision ID: kyle@canonical.com-20151001223645-vcgobopu6ffp6kct
Vendor go-trust-store, trust-store bindings for Go.

Signed-off-by: Kyle Fazzari <kyle@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2015 Canonical Ltd.
 
2
 *
 
3
 * This file is part of go-trust-store.
 
4
 *
 
5
 * go-trust-store is free software: you can redistribute it and/or modify it
 
6
 * under the terms of the GNU Lesser General Public License version 3, as
 
7
 * published by the Free Software Foundation.
 
8
 *
 
9
 * go-trust-store is distributed in the hope that it will be useful, but WITHOUT
 
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
11
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 
12
 * details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public License
 
15
 * along with go-trust-store. If not, see <http://www.gnu.org/licenses/>.
 
16
 *
 
17
 * Authored by: Kyle Fazzari <kyle@canonical.com>
 
18
 */
 
19
 
 
20
package trust
 
21
 
 
22
// #include <stdlib.h>
 
23
// #include <sys/types.h>
 
24
// #include "request_shim.h"
 
25
import "C"
 
26
 
 
27
import (
 
28
        "runtime"
 
29
        "time"
 
30
        "unsafe"
 
31
)
 
32
 
 
33
// Feature represents a trusted-helper-specific feature to which access is
 
34
// requested via the trust store.
 
35
type Feature uint64
 
36
 
 
37
// Request represents a timestamped permission request and answer to be stored
 
38
// in the trust store.
 
39
type Request struct {
 
40
        Answer  Answer
 
41
        From    string
 
42
        Feature Feature
 
43
        When    time.Time
 
44
}
 
45
 
 
46
// FromShim converts a shim request into a Request.
 
47
func (request *Request) FromShim(shimPointer unsafe.Pointer) {
 
48
        shim := (*C.Request)(shimPointer)
 
49
 
 
50
        request.Answer = AnswerFromShim(int(shim.answer))
 
51
        request.From = C.GoString(shim.from)
 
52
        request.Feature = Feature(shim.feature)
 
53
        request.When = time.Unix(int64(shim.when.seconds),
 
54
                int64(shim.when.nanoseconds))
 
55
}
 
56
 
 
57
// ToShim converts a Request into a shim request.
 
58
func (request Request) ToShim() unsafe.Pointer {
 
59
        shim := new(C.Request)
 
60
 
 
61
        shim.answer = C.Answer(request.Answer.ToShim())
 
62
        shim.from = C.CString(request.From)
 
63
        shim.feature = C.uint64_t(request.Feature)
 
64
        shim.when.seconds = C.int64_t(request.When.Unix())
 
65
        shim.when.nanoseconds = C.int32_t(request.When.Nanosecond())
 
66
 
 
67
        runtime.SetFinalizer(shim, destroyRequestShim)
 
68
 
 
69
        return unsafe.Pointer(shim)
 
70
}
 
71
 
 
72
// destroyRequestShim frees the resources of a shim request.
 
73
func destroyRequestShim(shim *C.Request) {
 
74
        C.free(unsafe.Pointer(shim.from))
 
75
}