diff options
Diffstat (limited to 'auth/token.go')
| -rw-r--r-- | auth/token.go | 57 | 
1 files changed, 57 insertions, 0 deletions
| 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) +} | 
