示例#1
0
文件: github.go 项目: nildev/account
// 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
}
示例#2
0
文件: github.go 项目: nildev/account
// 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
}
示例#3
0
文件: types.go 项目: nildev/account
// 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))
}
示例#4
0
文件: types.go 项目: nildev/account
// 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))
}
示例#5
0
文件: types.go 项目: nildev/account
// 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))
}
示例#6
0
文件: github.go 项目: nildev/account
// 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
}
示例#7
0
文件: mongodb.go 项目: nildev/account
// 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
}