本文整理汇总了Golang中gopkg/in/macaroon-bakery/v1/bakery.NewService函数的典型用法代码示例。如果您正苦于以下问题:Golang NewService函数的具体用法?Golang NewService怎么用?Golang NewService使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewService函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: targetService
// targetService implements a "target service", representing
// an arbitrary web service that wants to delegate authorization
// to third parties.
func targetService(endpoint, authEndpoint string, authPK *bakery.PublicKey) (http.Handler, error) {
key, err := bakery.GenerateKey()
if err != nil {
return nil, err
}
pkLocator := bakery.NewPublicKeyRing()
svc, err := bakery.NewService(bakery.NewServiceParams{
Key: key,
Location: endpoint,
Locator: pkLocator,
})
if err != nil {
return nil, err
}
log.Printf("adding public key for location %s: %v", authEndpoint, authPK)
pkLocator.AddPublicKeyForLocation(authEndpoint, true, authPK)
mux := http.NewServeMux()
srv := &targetServiceHandler{
svc: svc,
authEndpoint: authEndpoint,
}
mux.HandleFunc("/gold/", srv.serveGold)
mux.HandleFunc("/silver/", srv.serveSilver)
return mux, nil
}
示例2: TestLoginDischargerError
func (s *suite) TestLoginDischargerError(c *gc.C) {
var d *bakerytest.InteractiveDischarger
d = bakerytest.NewInteractiveDischarger(nil, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
d.FinishInteraction(w, r, nil, errors.New("test error"))
},
))
defer d.Close()
svc, err := bakery.NewService(bakery.NewServiceParams{
Location: "here",
Locator: d,
})
c.Assert(err, gc.IsNil)
m, err := svc.NewMacaroon("", nil, []checkers.Caveat{{
Location: d.Location(),
Condition: "something",
}})
c.Assert(err, gc.IsNil)
client := httpbakery.NewClient()
client.VisitWebPage = func(u *url.URL) error {
c.Logf("visiting %s", u)
var c httprequest.Client
return c.Get(u.String(), nil)
}
_, err = client.DischargeAll(m)
c.Assert(err, gc.ErrorMatches, `cannot get discharge from ".*": failed to acquire macaroon after waiting: third party refused discharge: test error`)
}
示例3: TestFormTitle
func (s *formSuite) TestFormTitle(c *gc.C) {
d := &formDischarger{}
d.discharger = bakerytest.NewInteractiveDischarger(nil, http.HandlerFunc(d.login))
defer d.discharger.Close()
d.discharger.Mux.Handle("/form", http.HandlerFunc(d.form))
svc, err := bakery.NewService(bakery.NewServiceParams{
Locator: testLocator{
loc: d.discharger.Location(),
locator: d.discharger,
},
})
c.Assert(err, gc.IsNil)
for i, test := range formTitleTests {
c.Logf("%d. %s", i, test.host)
m, err := svc.NewMacaroon("", nil, []checkers.Caveat{{
Location: "https://" + test.host,
Condition: "test condition",
}})
c.Assert(err, gc.Equals, nil)
client := httpbakery.NewClient()
client.Client.Transport = httptesting.URLRewritingTransport{
MatchPrefix: "https://" + test.host,
Replace: d.discharger.Location(),
RoundTripper: http.DefaultTransport,
}
f := new(titleTestFiller)
form.SetUpAuth(client, f)
ms, err := client.DischargeAll(m)
c.Assert(err, gc.IsNil)
c.Assert(len(ms), gc.Equals, 2)
c.Assert(f.title, gc.Equals, test.expect)
}
}
示例4: TestFormLogin
func (s *formSuite) TestFormLogin(c *gc.C) {
d := &formDischarger{}
d.discharger = bakerytest.NewInteractiveDischarger(nil, http.HandlerFunc(d.login))
defer d.discharger.Close()
d.discharger.Mux.Handle("/form", http.HandlerFunc(d.form))
svc, err := bakery.NewService(bakery.NewServiceParams{
Locator: d.discharger,
})
c.Assert(err, gc.IsNil)
for i, test := range formLoginTests {
c.Logf("%d. %s", i, test.about)
d.dischargeOptions = test.opts
m, err := svc.NewMacaroon("", nil, []checkers.Caveat{{
Location: d.discharger.Location(),
Condition: "test condition",
}})
c.Assert(err, gc.Equals, nil)
client := httpbakery.NewClient()
h := defaultFiller
if test.filler != nil {
h = test.filler
}
client.VisitWebPage = test.fallback
form.SetUpAuth(client, h)
ms, err := client.DischargeAll(m)
if test.expectError != "" {
c.Assert(err, gc.ErrorMatches, test.expectError)
continue
}
c.Assert(err, gc.IsNil)
c.Assert(len(ms), gc.Equals, 2)
}
}
示例5: NewDischarger
// NewDischarger returns a new third party caveat discharger
// which uses the given function to check caveats.
// The cond and arg arguments to the function are as returned
// by checkers.ParseCaveat.
//
// If locator is non-nil, it will be used to find public keys
// for any third party caveats returned by the checker.
//
// Calling this function has the side-effect of setting
// InsecureSkipVerify in http.DefaultTransport.TLSClientConfig
// until all the dischargers are closed.
func NewDischarger(
locator bakery.PublicKeyLocator,
checker func(req *http.Request, cond, arg string) ([]checkers.Caveat, error),
) *Discharger {
mux := http.NewServeMux()
server := httptest.NewTLSServer(mux)
svc, err := bakery.NewService(bakery.NewServiceParams{
Location: server.URL,
Locator: locator,
})
if err != nil {
panic(err)
}
checker1 := func(req *http.Request, cavId, cav string) ([]checkers.Caveat, error) {
cond, arg, err := checkers.ParseCaveat(cav)
if err != nil {
return nil, err
}
return checker(req, cond, arg)
}
httpbakery.AddDischargeHandler(mux, "/", svc, checker1)
startSkipVerify()
return &Discharger{
Service: svc,
server: server,
}
}
示例6: TestInteractiveDischargerURL
func (s *suite) TestInteractiveDischargerURL(c *gc.C) {
var d *bakerytest.InteractiveDischarger
d = bakerytest.NewInteractiveDischarger(nil, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, d.URL("/redirect", r), http.StatusFound)
},
))
defer d.Close()
d.Mux.Handle("/redirect", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
d.FinishInteraction(w, r, nil, nil)
},
))
svc, err := bakery.NewService(bakery.NewServiceParams{
Location: "here",
Locator: d,
})
c.Assert(err, gc.IsNil)
m, err := svc.NewMacaroon("", nil, []checkers.Caveat{{
Location: d.Location(),
Condition: "something",
}})
c.Assert(err, gc.IsNil)
client := httpbakery.NewClient()
client.VisitWebPage = func(u *url.URL) error {
var c httprequest.Client
return c.Get(u.String(), nil)
}
ms, err := client.DischargeAll(m)
c.Assert(err, gc.IsNil)
c.Assert(ms, gc.HasLen, 2)
err = svc.Check(ms, failChecker)
c.Assert(err, gc.IsNil)
}
示例7: TestMacaraq
func (*suite) TestMacaraq(c *gc.C) {
checked := false
d := bakerytest.NewDischarger(nil, func(_ *http.Request, cond, arg string) ([]checkers.Caveat, error) {
if cond != "something" {
return nil, fmt.Errorf("unexpected 3rd party cond")
}
checked = true
return nil, nil
})
bsvc, err := bakery.NewService(bakery.NewServiceParams{
Location: "here",
Locator: d,
})
c.Assert(err, gc.IsNil)
svc := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.ParseForm()
_, checkErr := httpbakery.CheckRequest(bsvc, req, nil, checkers.New())
if checkErr == nil {
w.Header().Set("Content-Type", "application/json")
data, err := json.Marshal(req.Form)
c.Check(err, gc.IsNil)
w.Write(data)
return
}
m, err := bsvc.NewMacaroon("", nil, []checkers.Caveat{{
Location: d.Service.Location(),
Condition: "something",
}})
c.Check(err, gc.IsNil)
httpbakery.WriteDischargeRequiredError(w, m, "/", checkErr)
}))
fset := flag.NewFlagSet("http", flag.ContinueOnError)
ctxt, params, err := newContext(fset, []string{
svc.URL,
"x=y",
})
c.Assert(err, gc.IsNil)
client := httpbakery.NewClient()
resp, err := ctxt.doRequest(client, nil)
c.Assert(err, gc.IsNil)
defer resp.Body.Close()
c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
c.Assert(checked, jc.IsTrue)
var stdout bytes.Buffer
err = showResponse(params, resp, &stdout)
c.Assert(err, gc.IsNil)
c.Assert(stdout.String(), gc.Equals, `{
x: [
"y"
]
}
`)
}
示例8: newService
func newService(location string, locator bakery.PublicKeyLocator) *bakery.Service {
svc, err := bakery.NewService(bakery.NewServiceParams{
Location: location,
Locator: locator,
})
if err != nil {
panic(err)
}
return svc
}
示例9: SetUpSuite
func (s *agentSuite) SetUpSuite(c *gc.C) {
key, err := bakery.GenerateKey()
c.Assert(err, gc.IsNil)
s.dischargeKey = &key.Public
c.Assert(err, gc.IsNil)
bak, err := bakery.NewService(bakery.NewServiceParams{
Key: key,
})
c.Assert(err, gc.IsNil)
s.discharger = &Discharger{
Bakery: bak,
}
s.server = s.discharger.Serve()
s.bakery, err = bakery.NewService(bakery.NewServiceParams{
Locator: bakery.PublicKeyLocatorMap{
s.discharger.URL: &key.Public,
},
})
}
示例10: addThirdPartyCaveat
func (ctx condContext) addThirdPartyCaveat(m *macaroon.Macaroon, location, condition string) error {
agent, err := bakery.NewService(bakery.NewServiceParams{
// TODO: persistent key pair for client
Locator: ctx,
})
if err != nil {
return err
}
return agent.AddCaveat(m, checkers.Caveat{Location: location, Condition: condition})
}
示例11: authService
// authService implements an authorization service,
// that can discharge third-party caveats added
// to other macaroons.
func authService(endpoint string, key *bakery.KeyPair) (http.Handler, error) {
svc, err := bakery.NewService(bakery.NewServiceParams{
Location: endpoint,
Key: key,
Locator: bakery.NewPublicKeyRing(),
})
if err != nil {
return nil, err
}
mux := http.NewServeMux()
httpbakery.AddDischargeHandler(mux, "/", svc, thirdPartyChecker)
return mux, nil
}
示例12: ExpireStorageAt
// ExpireStorageAt implements authentication.ExpirableStorageBakeryService.
func (s *expirableStorageBakeryService) ExpireStorageAt(t time.Time) (authentication.ExpirableStorageBakeryService, error) {
store := s.store.ExpireAt(t)
service, err := bakery.NewService(bakery.NewServiceParams{
Location: s.Location(),
Store: store,
Key: s.key,
Locator: s.locator,
})
if err != nil {
return nil, errors.Trace(err)
}
return &expirableStorageBakeryService{service, s.key, store, s.locator}, nil
}
示例13: TestDischargerTwoLevels
func (s *suite) TestDischargerTwoLevels(c *gc.C) {
d1checker := func(_ *http.Request, cond, arg string) ([]checkers.Caveat, error) {
if cond != "xtrue" {
return nil, fmt.Errorf("caveat refused")
}
return nil, nil
}
d1 := bakerytest.NewDischarger(nil, d1checker)
defer d1.Close()
d2checker := func(_ *http.Request, cond, arg string) ([]checkers.Caveat, error) {
return []checkers.Caveat{{
Location: d1.Location(),
Condition: "x" + cond,
}}, nil
}
d2 := bakerytest.NewDischarger(d1, d2checker)
defer d2.Close()
locator := bakery.PublicKeyLocatorMap{
d1.Location(): d1.Service.PublicKey(),
d2.Location(): d2.Service.PublicKey(),
}
c.Logf("map: %s", locator)
svc, err := bakery.NewService(bakery.NewServiceParams{
Location: "here",
Locator: locator,
})
c.Assert(err, gc.IsNil)
m, err := svc.NewMacaroon("", nil, []checkers.Caveat{{
Location: d2.Location(),
Condition: "true",
}})
c.Assert(err, gc.IsNil)
ms, err := s.client.DischargeAll(m)
c.Assert(err, gc.IsNil)
c.Assert(ms, gc.HasLen, 3)
err = svc.Check(ms, failChecker)
c.Assert(err, gc.IsNil)
err = svc.AddCaveat(m, checkers.Caveat{
Location: d2.Location(),
Condition: "nope",
})
c.Assert(err, gc.IsNil)
ms, err = s.client.DischargeAll(m)
c.Assert(err, gc.ErrorMatches, `cannot get discharge from "https://[^"]*": third party refused discharge: cannot discharge: caveat refused`)
c.Assert(ms, gc.HasLen, 0)
}
示例14: newService
func newService(c *gc.C, location string, locator bakery.PublicKeyLocatorMap) *bakery.Service {
keyPair, err := bakery.GenerateKey()
c.Assert(err, gc.IsNil)
svc, err := bakery.NewService(bakery.NewServiceParams{
Location: location,
Key: keyPair,
Locator: locator,
})
c.Assert(err, gc.IsNil)
if locator != nil {
locator[location] = &keyPair.Public
}
return svc
}
示例15: newMockAPI
func newMockAPI() (*mockapi, error) {
kp, err := bakery.GenerateKey()
if err != nil {
return nil, errors.Trace(err)
}
svc, err := bakery.NewService(bakery.NewServiceParams{
Location: "omnibus",
Key: kp,
})
if err != nil {
return nil, errors.Trace(err)
}
return &mockapi{
service: svc,
}, nil
}