1// Copyright 2015 Keybase, Inc. All rights reserved. Use of
2// this source code is governed by the included BSD license.
3
4// ProveCheck looks for an active proof in the logged in user's id
5// table for a service, username pair.
6
7package engine
8
9import (
10	"fmt"
11
12	"github.com/keybase/client/go/libkb"
13	keybase1 "github.com/keybase/client/go/protocol/keybase1"
14)
15
16// ProveCheck is an engine.
17type ProveCheck struct {
18	libkb.Contextified
19	sigID     keybase1.SigID
20	found     bool
21	status    keybase1.ProofStatus
22	state     keybase1.ProofState
23	proofText string
24}
25
26// NewProveCheck creates a ProveCheck engine.
27func NewProveCheck(g *libkb.GlobalContext, sigID keybase1.SigID) *ProveCheck {
28	return &ProveCheck{
29		Contextified: libkb.NewContextified(g),
30		sigID:        sigID,
31	}
32}
33
34// Name is the unique engine name.
35func (e *ProveCheck) Name() string {
36	return "ProveCheck"
37}
38
39// GetPrereqs returns the engine prereqs.
40func (e *ProveCheck) Prereqs() Prereqs {
41	return Prereqs{Device: true}
42}
43
44// RequiredUIs returns the required UIs.
45func (e *ProveCheck) RequiredUIs() []libkb.UIKind {
46	return []libkb.UIKind{}
47}
48
49// SubConsumers returns the other UI consumers for this engine.
50func (e *ProveCheck) SubConsumers() []libkb.UIConsumer {
51	return nil
52}
53
54// Run starts the engine.
55func (e *ProveCheck) Run(m libkb.MetaContext) error {
56	found, status, state, err := libkb.CheckPosted(m, e.sigID)
57	if err != nil {
58		return err
59	}
60	e.found = found
61	e.status = status
62	e.state = state
63
64	m.Debug("looking for ChainLink for %s", e.sigID)
65	me, err := libkb.LoadMe(libkb.NewLoadUserArgWithMetaContext(m).WithPublicKeyOptional())
66	if err != nil {
67		return err
68	}
69	link := me.LinkFromSigID(e.sigID)
70	if link == nil {
71		return fmt.Errorf("no chain link found for %s", e.sigID)
72	}
73	m.Debug("chain link found: (%T)", link.Typed())
74	if rlink, ok := link.Typed().(libkb.RemoteProofChainLink); ok {
75		e.proofText = rlink.ProofText()
76		m.Debug("chain link proof text: %q", e.proofText)
77	} else {
78		m.Warning("chain link had invalid type: %T", link.Typed())
79	}
80	return nil
81}
82
83func (e *ProveCheck) Results() (found bool, status keybase1.ProofStatus, state keybase1.ProofState, proofText string) {
84	return e.found, e.status, e.state, e.proofText
85}
86