Esempio n. 1
0
// Data reader
func (gr *gitHubReader) Data() (interface{}, error) {
	if gr.token == nil {
		return nil, errors.Trace(errors.Errorf("Use Token() first to exchange authCode for valid token"))
	}

	return gr.user, nil
}
Esempio n. 2
0
// Avatar reader
func (gr *gitHubReader) Avatar() (*string, error) {
	if gr.token == nil {
		return nil, errors.Trace(errors.Errorf("Use Token() first to exchange authCode for valid token"))
	}

	return gr.user.AvatarURL, nil
}
Esempio n. 3
0
// MakeReaderWithToken creates reader based on requested provider with given token
func MakeReaderWithToken(provider string, token oauth2.Token) (AccountReader, error) {
	switch provider {
	case PROVIDER_GITHUB:
		return makeGitHubProviderUsingEnv(&token), nil
	}

	return nil, errors.Trace(errors.Errorf("Provider [%s] is not supported", provider))
}
Esempio n. 4
0
// MakeReader creates reader based on requested provider
func MakeReader(provider string) (AccountReader, error) {
	switch provider {
	case PROVIDER_GITHUB:
		return makeGitHubProviderUsingEnv(nil), nil
	}

	return nil, errors.Trace(errors.Errorf("Provider [%s] is not supported", provider))
}
Esempio n. 5
0
// MakeRepository factory
func MakeRepositoryFromEnv() (AccountRepository, error) {
	storage := os.Getenv("ND_STORAGE")
	switch storage {
	case STORAGE_MONGODB:
		return makeMongoDB(), nil
	}

	return nil, errors.Trace(errors.Errorf("Storage [%s] is not supported", storage))
}
Esempio n. 6
0
// Token reader
func (gr *gitHubReader) Token(authCode string) (*oauth2.Token, error) {
	if authCode == "" {
		return nil, errors.Trace(errors.Errorf("authCode is empty!"))
	}

	conf := &oauth2.Config{
		ClientID:     gr.clientID,
		ClientSecret: gr.clientSecret,
		Scopes:       []string{},
		Endpoint:     oauthGithub.Endpoint,
	}

	token, err := conf.Exchange(oauth2.NoContext, authCode)

	if err != nil {
		logger.WithField("authCode", authCode).Errorf("%s", err)
		return nil, errors.Trace(err)
	}

	gr.token = token

	logger.WithField("authCode", authCode).Debugf("Got token, will expiry at [%s]", gr.token.Expiry)
	ts := oauth2.StaticTokenSource(gr.token)
	tc := oauth2.NewClient(oauth2.NoContext, ts)

	client := github.NewClient(tc)
	gr.user, _, err = client.Users.Get("")

	if err != nil {
		logger.WithField("authCode", authCode).Errorf("%s", err)
		return nil, errors.Trace(err)
	}
	logger.WithField("authCode", authCode).WithField("user", gr.user.Email).Debugf("[%s]", gr.user.String())

	return gr.token, nil
}
Esempio n. 7
0
// Save method
func (ds *mongodb) Save(acc *domain.Account) error {
	if acc == nil {
		return errors.Trace(errors.Errorf("acc is nil!"))
	}

	session, err := registry.CreateMongoDBClient()
	if err != nil {
		return err
	}

	collection := session.DB(registry.GetDatabaseName()).C(provision.TABLE_ACCOUNTS)
	_, err = collection.Upsert(bson.M{"email": acc.Email}, acc)

	if err != nil {
		return err
	}

	return nil
}