forked from prasmussen/gdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
701c7f1
commit 1973512
Showing
37 changed files
with
4,212 additions
and
4,213 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,67 @@ | ||
package auth | ||
|
||
import ( | ||
"golang.org/x/oauth2" | ||
"encoding/json" | ||
"os" | ||
"io/ioutil" | ||
"encoding/json" | ||
"golang.org/x/oauth2" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
|
||
func FileSource(path string, token *oauth2.Token, conf *oauth2.Config) oauth2.TokenSource { | ||
return &fileSource{ | ||
tokenPath: path, | ||
tokenSource: conf.TokenSource(oauth2.NoContext, token), | ||
} | ||
return &fileSource{ | ||
tokenPath: path, | ||
tokenSource: conf.TokenSource(oauth2.NoContext, token), | ||
} | ||
} | ||
|
||
type fileSource struct { | ||
tokenPath string | ||
tokenSource oauth2.TokenSource | ||
tokenPath string | ||
tokenSource oauth2.TokenSource | ||
} | ||
|
||
func (self *fileSource) Token() (*oauth2.Token, error) { | ||
token, err := self.tokenSource.Token() | ||
if err != nil { | ||
return token, err | ||
} | ||
token, err := self.tokenSource.Token() | ||
if err != nil { | ||
return token, err | ||
} | ||
|
||
// Save token to file | ||
SaveToken(self.tokenPath, token) | ||
// Save token to file | ||
SaveToken(self.tokenPath, token) | ||
|
||
return token, nil | ||
return token, nil | ||
} | ||
|
||
func ReadToken(path string) (*oauth2.Token, bool, error) { | ||
if !fileExists(path) { | ||
return nil, false, nil | ||
} | ||
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) | ||
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 | ||
} | ||
data, err := json.MarshalIndent(token, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = mkdir(path); err != nil { | ||
return err | ||
} | ||
if err = mkdir(path); err != nil { | ||
return err | ||
} | ||
|
||
// Write to temp file first | ||
tmpFile := path + ".tmp" | ||
err = ioutil.WriteFile(tmpFile, data, 0600) | ||
if err != nil { | ||
os.Remove(tmpFile) | ||
return err | ||
} | ||
// Write to temp file first | ||
tmpFile := path + ".tmp" | ||
err = ioutil.WriteFile(tmpFile, data, 0600) | ||
if err != nil { | ||
os.Remove(tmpFile) | ||
return err | ||
} | ||
|
||
// Move file to correct path | ||
return os.Rename(tmpFile, path) | ||
// Move file to correct path | ||
return os.Rename(tmpFile, path) | ||
} |
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,78 +1,78 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
"net/http" | ||
"golang.org/x/oauth2" | ||
"fmt" | ||
"golang.org/x/oauth2" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type authCodeFn func(string) func() string | ||
|
||
func NewFileSourceClient(clientId, clientSecret, tokenFile string, authFn authCodeFn) (*http.Client, error) { | ||
conf := getConfig(clientId, clientSecret) | ||
conf := getConfig(clientId, clientSecret) | ||
|
||
// Read cached token | ||
token, exists, err := ReadToken(tokenFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to read token: %s", err) | ||
} | ||
// Read cached token | ||
token, exists, err := ReadToken(tokenFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to read token: %s", 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) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to exchange auth code for token: %s", 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) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to exchange auth code for token: %s", err) | ||
} | ||
} | ||
|
||
return oauth2.NewClient( | ||
oauth2.NoContext, | ||
FileSource(tokenFile, token, conf), | ||
), nil | ||
return oauth2.NewClient( | ||
oauth2.NoContext, | ||
FileSource(tokenFile, token, conf), | ||
), nil | ||
} | ||
|
||
func NewRefreshTokenClient(clientId, clientSecret, refreshToken string) *http.Client { | ||
conf := getConfig(clientId, clientSecret) | ||
conf := getConfig(clientId, clientSecret) | ||
|
||
token := &oauth2.Token{ | ||
TokenType: "Bearer", | ||
RefreshToken: refreshToken, | ||
Expiry: time.Now(), | ||
} | ||
token := &oauth2.Token{ | ||
TokenType: "Bearer", | ||
RefreshToken: refreshToken, | ||
Expiry: time.Now(), | ||
} | ||
|
||
return oauth2.NewClient( | ||
oauth2.NoContext, | ||
conf.TokenSource(oauth2.NoContext, token), | ||
) | ||
return oauth2.NewClient( | ||
oauth2.NoContext, | ||
conf.TokenSource(oauth2.NoContext, token), | ||
) | ||
} | ||
|
||
func NewAccessTokenClient(clientId, clientSecret, accessToken string) *http.Client { | ||
conf := getConfig(clientId, clientSecret) | ||
conf := getConfig(clientId, clientSecret) | ||
|
||
token := &oauth2.Token{ | ||
TokenType: "Bearer", | ||
AccessToken: accessToken, | ||
} | ||
token := &oauth2.Token{ | ||
TokenType: "Bearer", | ||
AccessToken: accessToken, | ||
} | ||
|
||
return oauth2.NewClient( | ||
oauth2.NoContext, | ||
conf.TokenSource(oauth2.NoContext, token), | ||
) | ||
return oauth2.NewClient( | ||
oauth2.NoContext, | ||
conf.TokenSource(oauth2.NoContext, token), | ||
) | ||
} | ||
|
||
func getConfig(clientId, clientSecret string) *oauth2.Config { | ||
return &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", | ||
}, | ||
} | ||
return &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", | ||
}, | ||
} | ||
} |
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,22 +1,22 @@ | ||
package auth | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func mkdir(path string) error { | ||
dir := filepath.Dir(path) | ||
if fileExists(dir) { | ||
return nil | ||
} | ||
return os.Mkdir(dir, 0700) | ||
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 | ||
_, err := os.Stat(path) | ||
if err == nil { | ||
return true | ||
} | ||
return false | ||
} |
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,33 +1,32 @@ | ||
package cli | ||
|
||
|
||
type Context struct { | ||
args Arguments | ||
handlers []*Handler | ||
args Arguments | ||
handlers []*Handler | ||
} | ||
|
||
func (self Context) Args() Arguments { | ||
return self.args | ||
return self.args | ||
} | ||
|
||
func (self Context) Handlers() []*Handler { | ||
return self.handlers | ||
return self.handlers | ||
} | ||
|
||
type Arguments map[string]interface{} | ||
|
||
func (self Arguments) String(key string) string { | ||
return self[key].(string) | ||
return self[key].(string) | ||
} | ||
|
||
func (self Arguments) Int64(key string) int64 { | ||
return self[key].(int64) | ||
return self[key].(int64) | ||
} | ||
|
||
func (self Arguments) Bool(key string) bool { | ||
return self[key].(bool) | ||
return self[key].(bool) | ||
} | ||
|
||
func (self Arguments) StringSlice(key string) []string { | ||
return self[key].([]string) | ||
return self[key].([]string) | ||
} |
Oops, something went wrong.