aboutsummaryrefslogtreecommitdiffstats
path: root/auth
diff options
context:
space:
mode:
authorPetter Rasmussen2016-01-18 21:54:26 +0100
committerPetter Rasmussen2016-01-18 21:54:26 +0100
commit4f4152ccf32acbd392c7d80e45834ca1f3ea2d62 (patch)
treea61c9e6fcb6ede8c659ac8d76b491dbef5edafcc /auth
parente60833f88408139c8a92c3de9e8bfb87f295433e (diff)
downloadgdrive-4f4152ccf32acbd392c7d80e45834ca1f3ea2d62.tar.bz2
Simplify drive wrapper, s/client/auth/
Diffstat (limited to 'auth')
-rw-r--r--auth/oauth.go40
-rw-r--r--auth/token.go57
-rw-r--r--auth/util.go22
3 files changed, 119 insertions, 0 deletions
diff --git a/auth/oauth.go b/auth/oauth.go
new file mode 100644
index 0000000..b8f1d47
--- /dev/null
+++ b/auth/oauth.go
@@ -0,0 +1,40 @@
+package auth
+
+import (
+ "net/http"
+ "golang.org/x/oauth2"
+)
+
+type authCodeFn func(string) func() string
+
+func NewOauthClient(clientId, clientSecret, tokenFile string, authFn authCodeFn) (*http.Client, error) {
+ conf := &oauth2.Config{
+ ClientID: clientId,
+ ClientSecret: clientSecret,
+ Scopes: []string{"https://www.googleapis.com/auth/drive"},
+ RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
+ Endpoint: oauth2.Endpoint{
+ AuthURL: "https://accounts.google.com/o/oauth2/auth",
+ TokenURL: "https://accounts.google.com/o/oauth2/token",
+ },
+ }
+
+ // Read cached token
+ token, exists, err := ReadToken(tokenFile)
+ if err != nil {
+ return nil, err
+ }
+
+ // Require auth code if token file does not exist
+ // or refresh token is missing
+ if !exists || token.RefreshToken == "" {
+ authUrl := conf.AuthCodeURL("state", oauth2.AccessTypeOffline)
+ authCode := authFn(authUrl)()
+ token, err = conf.Exchange(oauth2.NoContext, authCode)
+ }
+
+ return oauth2.NewClient(
+ oauth2.NoContext,
+ FileSource(tokenFile, token, conf),
+ ), nil
+}
diff --git a/auth/token.go b/auth/token.go
new file mode 100644
index 0000000..926d9f6
--- /dev/null
+++ b/auth/token.go
@@ -0,0 +1,57 @@
+package auth
+
+import (
+ "golang.org/x/oauth2"
+ "encoding/json"
+ "io/ioutil"
+)
+
+
+func FileSource(path string, token *oauth2.Token, conf *oauth2.Config) oauth2.TokenSource {
+ return &fileSource{
+ tokenPath: path,
+ tokenSource: conf.TokenSource(oauth2.NoContext, token),
+ }
+}
+
+type fileSource struct {
+ tokenPath string
+ tokenSource oauth2.TokenSource
+}
+
+func (self *fileSource) Token() (*oauth2.Token, error) {
+ token, err := self.tokenSource.Token()
+ if err != nil {
+ return token, err
+ }
+
+ // Save token to file
+ SaveToken(self.tokenPath, token)
+
+ return token, nil
+}
+
+func ReadToken(path string) (*oauth2.Token, bool, error) {
+ if !fileExists(path) {
+ return nil, false, nil
+ }
+
+ content, err := ioutil.ReadFile(path)
+ if err != nil {
+ return nil, true, err
+ }
+ token := &oauth2.Token{}
+ return token, true, json.Unmarshal(content, token)
+}
+
+func SaveToken(path string, token *oauth2.Token) error {
+ data, err := json.MarshalIndent(token, "", " ")
+ if err != nil {
+ return err
+ }
+
+ if err = mkdir(path); err != nil {
+ return err
+ }
+ return ioutil.WriteFile(path, data, 0600)
+}
diff --git a/auth/util.go b/auth/util.go
new file mode 100644
index 0000000..b053c1f
--- /dev/null
+++ b/auth/util.go
@@ -0,0 +1,22 @@
+package auth
+
+import (
+ "os"
+ "path/filepath"
+)
+
+func mkdir(path string) error {
+ dir := filepath.Dir(path)
+ if fileExists(dir) {
+ return nil
+ }
+ return os.Mkdir(dir, 0700)
+}
+
+func fileExists(path string) bool {
+ _, err := os.Stat(path)
+ if err == nil {
+ return true
+ }
+ return false
+}