🔥 Removed 3rd-party profile providers
This commit is contained in:
parent
c677ba1e22
commit
399f5c934a
|
@ -1,78 +0,0 @@
|
||||||
package github
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
github "github.com/google/go-github/v41/github"
|
|
||||||
"golang.org/x/oauth2"
|
|
||||||
|
|
||||||
"source.toby3d.me/website/indieauth/internal/domain"
|
|
||||||
"source.toby3d.me/website/indieauth/internal/profile"
|
|
||||||
)
|
|
||||||
|
|
||||||
type githubProfileRepository struct{}
|
|
||||||
|
|
||||||
const ErrPrefix string = "github"
|
|
||||||
|
|
||||||
func NewGithubProfileRepository() profile.Repository {
|
|
||||||
return &githubProfileRepository{}
|
|
||||||
}
|
|
||||||
|
|
||||||
//nolint: cyclop
|
|
||||||
func (repo *githubProfileRepository) Get(ctx context.Context, token *oauth2.Token) (*domain.Profile, error) {
|
|
||||||
user, _, err := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(token))).Users.Get(ctx, "")
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%s: cannot get user info: %w", ErrPrefix, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
result := new(domain.Profile)
|
|
||||||
|
|
||||||
// NOTE(toby3d): Profile names.
|
|
||||||
if user.Name != nil {
|
|
||||||
result.Name = []string{*user.Name}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE(toby3d): Profile photos.
|
|
||||||
if user.AvatarURL != nil {
|
|
||||||
if u, err := domain.ParseURL(*user.AvatarURL); err == nil {
|
|
||||||
result.Photo = []*domain.URL{u}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE(toby3d): Profile URLs.
|
|
||||||
result.URL = make([]*domain.URL, 0)
|
|
||||||
|
|
||||||
var twitterURL *string
|
|
||||||
|
|
||||||
if user.TwitterUsername != nil {
|
|
||||||
u := "https://twitter.com/" + *user.TwitterUsername
|
|
||||||
twitterURL = &u
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, src := range []*string{
|
|
||||||
user.HTMLURL, // NOTE(toby3d): always available.
|
|
||||||
user.Blog,
|
|
||||||
twitterURL,
|
|
||||||
} {
|
|
||||||
if src == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
u, err := domain.ParseURL(*src)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
result.URL = append(result.URL, u)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE(toby3d): Profile Emails.
|
|
||||||
if user.Email != nil {
|
|
||||||
if email, err := domain.ParseEmail(*user.Email); err == nil {
|
|
||||||
result.Email = []*domain.Email{email}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
|
@ -1,90 +0,0 @@
|
||||||
package gitlab
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
gitlab "github.com/xanzy/go-gitlab"
|
|
||||||
"golang.org/x/oauth2"
|
|
||||||
|
|
||||||
"source.toby3d.me/website/indieauth/internal/domain"
|
|
||||||
"source.toby3d.me/website/indieauth/internal/profile"
|
|
||||||
)
|
|
||||||
|
|
||||||
type gitlabProfileRepository struct{}
|
|
||||||
|
|
||||||
const ErrPrefix string = "gitlab"
|
|
||||||
|
|
||||||
func NewGitlabProfileRepository() profile.Repository {
|
|
||||||
return &gitlabProfileRepository{}
|
|
||||||
}
|
|
||||||
|
|
||||||
//nolint: funlen, cyclop
|
|
||||||
func (repo *gitlabProfileRepository) Get(_ context.Context, token *oauth2.Token) (*domain.Profile, error) {
|
|
||||||
client, err := gitlab.NewClient(token.AccessToken)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%s: cannot create client: %w", ErrPrefix, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
user, _, err := client.Users.CurrentUser()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%s: cannot get user info: %w", ErrPrefix, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
result := new(domain.Profile)
|
|
||||||
|
|
||||||
// NOTE(toby3d): Profile names.
|
|
||||||
if user.Name != "" {
|
|
||||||
result.Name = []string{user.Name}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE(toby3d): Profile photos.
|
|
||||||
if user.AvatarURL != "" {
|
|
||||||
if u, err := domain.ParseURL(user.AvatarURL); err == nil {
|
|
||||||
result.Photo = []*domain.URL{u}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE(toby3d): Profile URLs.
|
|
||||||
result.URL = make([]*domain.URL, 0)
|
|
||||||
|
|
||||||
for _, src := range []string{
|
|
||||||
user.WebURL, // NOTE(toby3d): always available.
|
|
||||||
user.WebsiteURL,
|
|
||||||
"https://twitter.com/" + user.Twitter,
|
|
||||||
// TODO(toby3d): Skype field
|
|
||||||
// TODO(toby3d): LinkedIn field
|
|
||||||
} {
|
|
||||||
if src == "" || src == "https://twitter.com/" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
u, err := domain.ParseURL(user.WebsiteURL)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
result.URL = append(result.URL, u)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE(toby3d): Profile Emails.
|
|
||||||
result.Email = make([]*domain.Email, 0)
|
|
||||||
|
|
||||||
for _, src := range []string{
|
|
||||||
user.PublicEmail,
|
|
||||||
user.Email,
|
|
||||||
} {
|
|
||||||
if src == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
email, err := domain.ParseEmail(src)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
result.Email = append(result.Email, email)
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
|
@ -1,77 +0,0 @@
|
||||||
package mastodon
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
mastodon "github.com/mattn/go-mastodon"
|
|
||||||
"golang.org/x/oauth2"
|
|
||||||
|
|
||||||
"source.toby3d.me/website/indieauth/internal/domain"
|
|
||||||
"source.toby3d.me/website/indieauth/internal/profile"
|
|
||||||
)
|
|
||||||
|
|
||||||
type mastodonProfileRepository struct {
|
|
||||||
server string
|
|
||||||
}
|
|
||||||
|
|
||||||
const ErrPrefix string = "mastodon"
|
|
||||||
|
|
||||||
func NewMastodonProfileRepository(server string) profile.Repository {
|
|
||||||
return &mastodonProfileRepository{
|
|
||||||
server: server,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repo *mastodonProfileRepository) Get(ctx context.Context, token *oauth2.Token) (*domain.Profile, error) {
|
|
||||||
account, err := mastodon.NewClient(&mastodon.Config{
|
|
||||||
AccessToken: token.AccessToken,
|
|
||||||
ClientID: "",
|
|
||||||
ClientSecret: "",
|
|
||||||
Server: repo.server,
|
|
||||||
}).GetAccountCurrentUser(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%s: cannot get account info: %w", ErrPrefix, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
result := new(domain.Profile)
|
|
||||||
|
|
||||||
// NOTE(toby3d): Profile names.
|
|
||||||
if account.DisplayName != "" {
|
|
||||||
result.Name = []string{account.DisplayName}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE(toby3d): Profile photos.
|
|
||||||
if account.Avatar != "" {
|
|
||||||
if u, err := domain.ParseURL(account.Avatar); err == nil {
|
|
||||||
result.Photo = []*domain.URL{u}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE(toby3d): Profile URLs.
|
|
||||||
result.URL = make([]*domain.URL, 0)
|
|
||||||
|
|
||||||
// NOTE(toby3d): must be always available
|
|
||||||
if account.URL != "" {
|
|
||||||
if u, err := domain.ParseURL(account.URL); err == nil {
|
|
||||||
result.URL = append(result.URL, u)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range account.Fields {
|
|
||||||
// NOTE(toby3d): ignore non-verified fields that contain either
|
|
||||||
// free-form text or links in them have not yet been verified.
|
|
||||||
if account.Fields[i].VerifiedAt.IsZero() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
u, err := domain.ParseURL(account.Fields[i].Value)
|
|
||||||
if err == nil {
|
|
||||||
result.URL = append(result.URL, u)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WARN(toby3d): Mastodon does not provide an email via API.
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
Loading…
Reference in New Issue