コード例 #1
0
ファイル: authorization.go プロジェクト: masom/doorbot
// AuthenticateAdministrator wraps the authentication logic for administrators.
func AuthenticateAdministrator(r doorbot.Repositories, token string) (*doorbot.Administrator, error) {

	var administrator *doorbot.Administrator

	ar := r.AdministratorRepository()
	aar := r.AdministratorAuthenticationRepository()

	authentication, err := aar.FindByProviderIDAndToken(r.DB(), ProviderAPIToken, token)

	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
		}).Error("Auth::Authorization::AuthenticationAdministrator database error")

		return administrator, errors.New("Not authorized")
	}

	if authentication == nil {
		log.WithFields(log.Fields{
			"token": token,
		}).Warn("Doorbot::Authorization::AuthenticateAdministrator token not found")

		return administrator, errors.New("Not authorized")
	}

	administrator, err = ar.Find(r.DB(), authentication.AdministratorID)

	if err != nil {
		log.Println(err)
		return administrator, errors.New("Doorbot::AuthenticateAdministrator ")
	}

	return administrator, nil
}