package auth import ( "bytes" "context" "encoding/gob" "net/http" "github.com/danieljoos/wincred" "golang.org/x/oauth2" ) type Credentials struct { GoogleToken *oauth2.Token GUID string UofgPass string } const credential_name = "uofgsync" func read_credentials() (*Credentials, error) { // read from windows credential manager cred, err := wincred.GetGenericCredential(credential_name) if err != nil { return nil, err } // deserialize the credentials var c Credentials dec := gob.NewDecoder(bytes.NewReader(cred.CredentialBlob)) if err := dec.Decode(&c); err != nil { return nil, err } return &c, nil } func save_credentials(credentials Credentials) error { // serialize the credentials var buf bytes.Buffer enc := gob.NewEncoder(&buf) if err := enc.Encode(credentials); err != nil { return err } // save to windows credential manager cred := wincred.NewGenericCredential(credential_name) cred.CredentialBlob = buf.Bytes() if err := cred.Write(); err != nil { return err } return nil } func GetCredentials(config *oauth2.Config) (*Credentials, error) { creds, err := read_credentials() if err != nil { google := fetch_google_token(config) guid, uofg_pass, err := collect_uofg() if err != nil { return nil, err } creds = &Credentials{ GoogleToken: google, GUID: guid, UofgPass: uofg_pass, } save_credentials(*creds) } return creds, nil } func GetGoogleClient(config *oauth2.Config, token *oauth2.Token) *http.Client { return config.Client(context.Background(), token) }