drivel/credentials.go

89 lines
2.4 KiB
Go
Raw Normal View History

2019-01-01 19:35:22 +01:00
package main
import (
"fmt"
2020-10-11 13:14:57 +02:00
goutil "git.gutmet.org/goutil.git/misc"
2019-01-01 19:35:22 +01:00
"github.com/dghubble/oauth1"
"io/ioutil"
"net/http"
"os"
"os/user"
"path"
)
const (
AppDir = ".drivel" //inside home dir
fileConsumerKey = "ConsumerKey"
fileConsumerSecret = "ConsumerSecret"
fileAccessToken = "AccessToken"
fileAccessTokenSecret = "AccessTokenSecret"
registerAppURL = "https://apps.twitter.com/app/new"
)
type credentials struct {
consumerKey string
consumerSecret string
accessToken string
accessTokenSecret string
}
func (c *credentials) writeCredentials(appDir string) {
2019-01-01 19:35:22 +01:00
write := func(file string, s string) {
optLogFatal("writeToAppDir", ioutil.WriteFile(path.Join(appDir, file), []byte(s), 0640))
}
write(fileConsumerKey, c.consumerKey)
write(fileConsumerSecret, c.consumerSecret)
write(fileAccessToken, c.accessToken)
write(fileAccessTokenSecret, c.accessTokenSecret)
}
func readCredentials(appDir string) *credentials {
2019-01-01 19:35:22 +01:00
read := func(f string) string {
s, err := goutil.ReadFile(path.Join(appDir, f))
optLogFatal("readFromAppDir", err)
return s
}
ck := read(fileConsumerKey)
cs := read(fileConsumerSecret)
at := read(fileAccessToken)
ats := read(fileAccessTokenSecret)
return &credentials{ck, cs, at, ats}
}
func createAppDir(appDir string) {
ask := func(s string) string {
a, err := goutil.AskFor(s)
optLogFatal("createAppDir", err)
return a
}
fmt.Println("Did not find " + appDir + ", creating.")
2020-08-08 11:52:42 +02:00
fmt.Println("Go to " + registerAppURL + " to register a developer account, register a new app and generate access tokens with read & write permissions\n")
2019-01-01 19:35:22 +01:00
err := os.MkdirAll(appDir, 0755)
optLogFatal("createAppDir", err)
2020-08-08 11:52:42 +02:00
ck := ask("Consumer Key/API Key")
cs := ask("Consumer Secret/API Secret")
2019-01-01 19:35:22 +01:00
at := ask("Access Token")
ats := ask("Access Token Secret")
c := credentials{ck, cs, at, ats}
c.writeCredentials(appDir)
2019-01-01 19:35:22 +01:00
}
func appDir() string {
log := func(err error) { optLogFatal("appDir", err) }
2019-01-01 19:35:22 +01:00
currentUser, err := user.Current()
log(err)
homeDir := currentUser.HomeDir
appDir := path.Join(homeDir, AppDir)
if !goutil.PathExists(appDir) {
createAppDir(appDir)
}
return appDir
2019-01-01 19:35:22 +01:00
}
func getClient() *http.Client {
c := readCredentials(appDir())
2019-01-01 19:35:22 +01:00
config := oauth1.NewConfig(c.consumerKey, c.consumerSecret)
token := oauth1.NewToken(c.accessToken, c.accessTokenSecret)
return config.Client(oauth1.NoContext, token)
}