Chromium Code Reviews| Index: server/config/authority.go |
| diff --git a/server/config/authority.go b/server/config/authority.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e8e7e915e69194343631092eaeb2ae7ced40971a |
| --- /dev/null |
| +++ b/server/config/authority.go |
| @@ -0,0 +1,102 @@ |
| +// Copyright 2016 The LUCI Authors. All rights reserved. |
|
iannucci
2017/01/07 20:12:15
maybe 'luci-config' top level folder and maybe 'se
|
| +// Use of this source code is governed under the Apache License, Version 2.0 |
| +// that can be found in the LICENSE file. |
| + |
| +package config |
| + |
| +import ( |
| + "encoding/json" |
| + "fmt" |
| + |
| + "github.com/luci/luci-go/common/errors" |
| + "github.com/luci/luci-go/server/auth" |
| +) |
| + |
| +// Authority is the authority that is requesting configurations. It can be |
| +// installed via WithAuthority. |
| +// |
| +// Authority marshals/unmarshals to/from a compact JSON representation. This is |
| +// used by the caching layer. |
| +type Authority int |
| + |
| +const ( |
| + // AsAnonymous requests config data as an anonymous user. |
| + // |
| + // Corresponds to auth.NoAuth. |
| + AsAnonymous Authority = iota |
| + |
| + // AsService requests config data as the currently-running service. |
| + // |
| + // Corresponds to auth.AsSelf. |
| + AsService |
| + |
| + // AsUser requests config data as the currently logged-in user. |
| + // |
| + // Corresponds to auth.AsUser. |
| + AsUser |
| +) |
| + |
| +// String implements fmt.Stringer. |
| +func (a Authority) String() string { |
|
iannucci
2017/01/07 20:12:15
use stringer tool? https://godoc.org/golang.org/x/
dnj
2017/01/10 03:25:57
Done.
|
| + switch a { |
| + case AsAnonymous: |
| + return "AsAnonymous" |
| + case AsService: |
| + return "AsService" |
| + case AsUser: |
| + return "AsUser" |
| + default: |
| + return fmt.Sprintf("Unknown(%d)", a) |
| + } |
| +} |
| + |
| +// rpcAuthorityKind returns the RPC authority associated with this authority |
| +// level. |
| +func (a Authority) rpcAuthorityKind() auth.RPCAuthorityKind { |
| + switch a { |
| + case AsAnonymous: |
| + return auth.NoAuth |
| + case AsService: |
| + return auth.AsSelf |
| + case AsUser: |
| + return auth.AsUser |
| + default: |
| + panic(fmt.Errorf("unknown config Authority (%d)", a)) |
| + } |
| +} |
| + |
| +// MarshalJSON implements encoding/json.Marshaler. |
| +func (a Authority) MarshalJSON() ([]byte, error) { |
| + switch a { |
| + case AsAnonymous: |
| + return json.Marshal("") |
| + case AsService: |
| + return json.Marshal("S") |
| + case AsUser: |
| + return json.Marshal("U") |
|
iannucci
2017/01/07 20:12:15
not sure it's worth calling json.Marshal here, con
dnj
2017/01/10 03:25:57
It would actually be []byte(`"U"`), which is a mis
|
| + default: |
| + return nil, errors.Reason("unknown authority: %(auth)v").D("auth", a).Err() |
| + } |
| +} |
| + |
| +// UnmarshalJSON implements encoding/json.Unmarshaler. |
| +func (a *Authority) UnmarshalJSON(d []byte) error { |
|
iannucci
2017/01/07 20:12:15
with constants mentioned above this becomes
swit
dnj
2017/01/10 03:25:57
Done.
|
| + var v string |
| + if err := json.Unmarshal(d, &v); err != nil { |
| + return errors.Annotate(err).Reason("failed to unmarshal JSON: %(value)v").D("value", d).Err() |
| + } |
| + |
| + switch v { |
| + case "": |
| + *a = AsAnonymous |
| + return nil |
| + case "S": |
| + *a = AsService |
| + return nil |
| + case "U": |
| + *a = AsUser |
| + return nil |
| + default: |
| + return errors.Reason("unknown authority JSON value: %(auth)v").D("auth", v).Err() |
| + } |
| +} |