-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Resolves #194
- Loading branch information
Showing
58 changed files
with
1,391 additions
and
1,079 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package bots | ||
|
||
import ( | ||
"github.com/coniks-sys/coniks-go/application" | ||
) | ||
|
||
// A TwitterConfig contains the address of the named UNIX socket | ||
// through which the bot and the CONIKS server communicate, | ||
// the OAuth information needed to authenticate the bot with Twitter, | ||
// and the bot's reserved Twitter handle. These values are specified | ||
// in a configuration file, which is read at initialization time. | ||
type TwitterConfig struct { | ||
CONIKSAddress string `toml:"coniks_address"` | ||
TwitterOAuth `toml:"twitter_oauth"` | ||
Handle string `toml:"twitter_bot_handle"` | ||
} | ||
|
||
// A TwitterOAuth contains the four secret values needed to authenticate | ||
// the bot with Twitter. These values are unique to each application | ||
// that uses the Twitter API to access an account's feed and direct | ||
// messages, and must be generated via Twitter's developer portal. | ||
type TwitterOAuth struct { | ||
ConsumerKey string | ||
ConsumerSecret string | ||
AccessToken string | ||
AccessSecret string | ||
} | ||
|
||
var _ application.AppConfig = (*TwitterConfig)(nil) | ||
|
||
// NewTwitterConfig initializes a new Twitter registration bot configuration | ||
// with the given server address, Twitter handle, and OAuth credentials. | ||
func NewTwitterConfig(addr, handle string, oauth TwitterOAuth) *TwitterConfig { | ||
var conf = TwitterConfig{ | ||
CONIKSAddress: addr, | ||
Handle: handle, | ||
TwitterOAuth: oauth, | ||
} | ||
|
||
return &conf | ||
} | ||
|
||
// Load initializes a Twitter registration proxy configuration from the | ||
// corresponding config file. | ||
func (conf *TwitterConfig) Load(file string) error { | ||
tmp, err := application.LoadConfig(file) | ||
if err != nil { | ||
return err | ||
} | ||
conf = tmp.(*TwitterConfig) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
coniksbots/twitterbot_test.go → application/bots/twitterbot_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package coniksbots | ||
package bots | ||
|
||
import ( | ||
"encoding/json" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/coniks-sys/coniks-go/application" | ||
"github.com/coniks-sys/coniks-go/protocol" | ||
) | ||
|
||
// CreateRegistrationMsg returns a JSON encoding of | ||
// a protocol.RegistrationRequest for the given (name, key) pair. | ||
func CreateRegistrationMsg(name string, key []byte) ([]byte, error) { | ||
return application.MarshalRequest(protocol.RegistrationType, | ||
&protocol.RegistrationRequest{ | ||
Username: name, | ||
Key: key, | ||
}) | ||
} | ||
|
||
// CreateKeyLookupMsg returns a JSON encoding of | ||
// a protocol.KeyLookupRequest for the given name. | ||
func CreateKeyLookupMsg(name string) ([]byte, error) { | ||
return application.MarshalRequest(protocol.KeyLookupType, | ||
&protocol.RegistrationRequest{ | ||
Username: name, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package application | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/coniks-sys/coniks-go/crypto/sign" | ||
"github.com/coniks-sys/coniks-go/utils" | ||
) | ||
|
||
// AppConfig is the generic type used to specify the configuration of | ||
// any kind of CONIKS application-level executable (e.g. key server, | ||
// client etc.). | ||
type AppConfig interface { | ||
Load(file string) error | ||
} | ||
|
||
// LoadSigningPubKey loads a public signing key at the given path | ||
// specified in the given config file. | ||
// If there is any parsing error or the key is malformed, | ||
// LoadSigningPubKey() returns an error with a nil key. | ||
func LoadSigningPubKey(path, file string) (sign.PublicKey, error) { | ||
signPath := utils.ResolvePath(path, file) | ||
signPubKey, err := ioutil.ReadFile(signPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("Cannot read signing key: %v", err) | ||
} | ||
if len(signPubKey) != sign.PublicKeySize { | ||
return nil, fmt.Errorf("Signing public-key must be 32 bytes (got %d)", len(signPubKey)) | ||
} | ||
return signPubKey, nil | ||
} | ||
|
||
// LoadConfig loads an application configuration from the given toml-encoded | ||
// file. If there is any decoding error, an LoadConfig() returns an error | ||
// with a nil config. | ||
func LoadConfig(file string) (AppConfig, error) { | ||
var conf AppConfig | ||
if _, err := toml.DecodeFile(file, &conf); err != nil { | ||
return nil, fmt.Errorf("Failed to load config: %v", err) | ||
} | ||
return conf, nil | ||
} | ||
|
||
// SaveConfig stores the given configuration conf in the given | ||
// file using toml encoding. | ||
// If there is any encoding or IO error, SaveConfig() returns an error. | ||
func SaveConfig(file string, conf AppConfig) error { | ||
var confBuf bytes.Buffer | ||
|
||
e := toml.NewEncoder(&confBuf) | ||
if err := e.Encode(conf); err != nil { | ||
return err | ||
} | ||
if err := utils.WriteFile(file, confBuf.Bytes(), 0644); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.