Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/seventv/api/internal/pprof"
"github.com/seventv/api/internal/rest"
"github.com/seventv/api/internal/svc/prometheus"
"github.com/seventv/api/internal/svc/youtube"
"github.com/seventv/common/events"
"github.com/seventv/common/mongo"
"github.com/seventv/common/mongo/indexing"
Expand Down Expand Up @@ -182,6 +183,14 @@ func main() {

{
gCtx.Inst().Loaders = loaders.New(gCtx, gCtx.Inst().Mongo, gCtx.Inst().Redis, gCtx.Inst().Query)
gCtx.Inst().YouTube, err = youtube.New(gCtx, youtube.YouTubeOptions{
APIKey: config.Platforms.YouTube.APIKey,
})
if err != nil {
zap.S().Errorw("failed to setup youtube instance",
"error", err,
)
}
}

wg := sync.WaitGroup{}
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ require (
go.mongodb.org/mongo-driver v1.9.1
go.uber.org/multierr v1.8.0
go.uber.org/zap v1.21.0
google.golang.org/api v0.81.0
)

require (
cloud.google.com/go/compute v1.6.1 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
Expand All @@ -40,7 +42,9 @@ require (
github.com/go-openapi/spec v0.20.6 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand All @@ -59,10 +63,15 @@ require (
github.com/urfave/cli/v2 v2.11.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.opencensus.io v0.23.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/tools v0.1.11 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
google.golang.org/grpc v1.46.2 // indirect
google.golang.org/protobuf v1.28.0 // indirect
)

Expand Down
186 changes: 182 additions & 4 deletions go.sum

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions internal/configure/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ type Config struct {
ClientSecret string `mapstructure:"client_secret" json:"client_secret"`
RedirectURI string `mapstructure:"redirect_uri" json:"redirect_uri"`
} `mapstructure:"twitch" json:"twitch"`
YouTube struct {
APIKey string `mapstructure:"api_key" json:"api_key"`
} `mapstructure:"youtube" json:"youtube"`
} `mapstructure:"platforms" json:"platforms"`

Limits struct {
Expand Down
2 changes: 2 additions & 0 deletions internal/instance/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package instance
import (
"github.com/seventv/api/internal/loaders"
"github.com/seventv/api/internal/svc/prometheus"
"github.com/seventv/api/internal/svc/youtube"
"github.com/seventv/common/events"
"github.com/seventv/common/mongo"
"github.com/seventv/common/redis"
Expand All @@ -19,6 +20,7 @@ type Instances struct {
MessageQueue messagequeue.Instance
Prometheus prometheus.Instance
Events events.Instance
YouTube youtube.Instance
Loaders loaders.Instance

Query *query.Query
Expand Down
3 changes: 3 additions & 0 deletions internal/rest/v2/routes/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func (r *Route) Config() rest.RouteConfig {
return rest.RouteConfig{
URI: "/auth",
Method: rest.GET,
Children: []rest.Route{
newYouTube(r.Ctx),
},
}
}

Expand Down
145 changes: 145 additions & 0 deletions internal/rest/v2/routes/auth/youtube.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package auth

import (
"context"
"encoding/hex"
"fmt"
"time"

"github.com/seventv/api/internal/global"
"github.com/seventv/api/internal/rest/rest"
"github.com/seventv/api/internal/rest/v3/middleware"
"github.com/seventv/common/errors"
"github.com/seventv/common/mongo"
"github.com/seventv/common/utils"
"go.mongodb.org/mongo-driver/bson"
"go.uber.org/zap"
"google.golang.org/api/youtube/v3"
)

type youtubeRoot struct {
Ctx global.Context
}

func newYouTube(gctx global.Context) rest.Route {
return &youtubeRoot{gctx}
}

func (r *youtubeRoot) Config() rest.RouteConfig {
return rest.RouteConfig{
URI: "/youtube",
Method: rest.GET,
Children: []rest.Route{
newYouTubeRequest(r.Ctx),
newYouTubeVerify(r.Ctx),
},
}
}

func (r *youtubeRoot) Handler(ctx *rest.Ctx) rest.APIError {
return errors.ErrUnknownRoute()
}

// Request Verification

type youtubeRequest struct {
Ctx global.Context
}

func newYouTubeRequest(gctx global.Context) rest.Route {
return &youtubeRequest{gctx}
}

func (r *youtubeRequest) Config() rest.RouteConfig {
return rest.RouteConfig{
URI: "/request-verification",
Method: rest.GET,
Middleware: []rest.Middleware{
middleware.Auth(r.Ctx),
},
}
}

func (r *youtubeRequest) Handler(ctx *rest.Ctx) rest.APIError {
if r.Ctx.Inst().YouTube == nil {
return errors.ErrMissingInternalDependency().SetDetail("YouTube API is not setup on this server")
}

channelID := utils.B2S(ctx.QueryArgs().Peek("channel_id"))
if channelID == "" {
return errors.ErrInvalidRequest().SetDetail("channel_id is a required query parameter")
}

// getters attempt to retrieve a youtube channel by different parameters
getters := []func(c context.Context, v string) (*youtube.Channel, error){
r.Ctx.Inst().YouTube.GetChannelByID,
r.Ctx.Inst().YouTube.GetChannelByUsername,
}

var (
channel *youtube.Channel
err error
)

for _, f := range getters {
channel, err = f(ctx, channelID)
if err == nil {
break
}
}

if channel == nil { // no channel was found with the id passed by the user
return errors.ErrUnknownUser().SetDetail("Unable to find YouTube channel")
}

if count, _ := r.Ctx.Inst().Mongo.Collection(mongo.CollectionNameUsers).CountDocuments(ctx, bson.M{
"connections.id": channel.Id,
}); count > 0 {
return errors.ErrInsufficientPrivilege().SetDetail("This channel is already bound to another user")
}

// Generate a random string that will be used to verify the requester owns the channel
tokenBytes, err := utils.GenerateRandomBytes(24)
if err != nil {
zap.S().Errorw("youtube, couldn't generate verification token", "error", err)
return errors.ErrInternalServerError()
}

token := hex.EncodeToString(tokenBytes)

actor, ok := ctx.GetActor()

if !ok {
return errors.ErrUnauthorized()
}

// Store this token in state
// it can now be used to claim ownership of the channel
if err = r.Ctx.Inst().Redis.SetEX(
ctx,
r.Ctx.Inst().Redis.ComposeKey("rest-v3", actor.ID.Hex(), channelID),
token,
time.Hour*2,
); err != nil {
zap.S().Errorw("youtube, failed to save state of verification token")
return errors.ErrInternalServerError()
}

result := verificationRequestResult{
Token: token,
VerificationString: fmt.Sprintf(`[7TV VERIFY]:"%s"`, token),
ManageChannelURL: fmt.Sprintf("https://studio.youtube.com/channel/%s/editing/details", channel.Id),
ChannelID: channel.Id,
Channel: channel,
}

return ctx.JSON(rest.OK, result)
}

type verificationRequestResult struct {
Token string `json:"token"`
VerificationString string `json:"verification_string"`
ManageChannelURL string `json:"manage_channel_url"`
ChannelID string `json:"channel_id"`
Channel *youtube.Channel `json:"channel"`
}
134 changes: 134 additions & 0 deletions internal/rest/v2/routes/auth/youtube.verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package auth

import (
"fmt"
"regexp"
"time"

"github.com/seventv/api/internal/global"
"github.com/seventv/api/internal/rest/rest"
"github.com/seventv/api/internal/rest/v3/middleware"
"github.com/seventv/common/errors"
"github.com/seventv/common/mongo"
"github.com/seventv/common/redis"
"github.com/seventv/common/structures/v3"
"github.com/seventv/common/utils"
"go.mongodb.org/mongo-driver/bson"
"go.uber.org/zap"
"google.golang.org/api/youtube/v3"
)

type youtubeVerify struct {
Ctx global.Context
}

func newYouTubeVerify(gctx global.Context) rest.Route {
return &youtubeVerify{gctx}
}

func (r *youtubeVerify) Config() rest.RouteConfig {
return rest.RouteConfig{
URI: "/verify",
Method: rest.GET,
Middleware: []rest.Middleware{
middleware.Auth(r.Ctx),
},
}
}
func (r *youtubeVerify) Handler(ctx *rest.Ctx) rest.APIError {
if r.Ctx.Inst().YouTube == nil {
return errors.ErrMissingInternalDependency().SetDetail("YouTube API is not setup on this server")
}

channelID := utils.B2S(ctx.QueryArgs().Peek("channel_id"))
if channelID == "" {
return errors.ErrInvalidRequest().SetDetail("channel_id is a required query parameter")
}

// Retrieve session
actor, ok := ctx.GetActor()
if !ok {
return errors.ErrUnauthorized()
}

rkey := r.Ctx.Inst().Redis.ComposeKey("rest-v3", actor.ID.Hex(), channelID)
tok, err := r.Ctx.Inst().Redis.Get(ctx, rkey)

if err != nil {
if err == redis.Nil {
return errors.ErrInsufficientPrivilege().SetDetail("There is no verification flow for that channel, or it expired")
}

return errors.ErrInternalServerError()
}

// Re-fetch the channel
channel, err := r.Ctx.Inst().YouTube.GetChannelByID(ctx, channelID)
if err != nil {
return errors.ErrUnknownUser().SetDetail("The channel associated with this verification flow is not available")
}

// Form a regex to parse out the verification token
regex, err := regexp.Compile(fmt.Sprintf(`\[7TV VERIFY\]:"(%s?)"`, tok))
if err != nil {
return errors.ErrInternalServerError()
}

// Check for a match with the description
if ok := regex.MatchString(channel.Snippet.Description); !ok {
return errors.ErrInvalidRequest().SetDetail("The token was not found in the channel's description. Please try again")
}

// Now we know the user controls the channel
// Let's create a youtube connection
tw, _, _ := actor.Connections.Twitch()

ucb := structures.NewUserConnectionBuilder(structures.UserConnection[structures.UserConnectionDataYoutube]{
ID: channel.Id,
Platform: structures.UserConnectionPlatformYouTube,
LinkedAt: time.Now(),
EmoteSlots: 250,
EmoteSetID: tw.EmoteSetID, // infer set from twitch conn if it exists
}).SetID(channel.Id).
SetPlatform(structures.UserConnectionPlatformYouTube).
SetLinkedAt(time.Now()).
SetActiveEmoteSet(tw.EmoteSetID).
SetData(structures.UserConnectionDataYoutube{
ID: channelID,
Title: channel.Snippet.Title,
Description: channel.Snippet.Description,
ViewCount: int64(channel.Statistics.ViewCount),
SubCount: int64(channel.Statistics.SubscriberCount),
})

ub := structures.NewUserBuilder(*actor)
ub.AddConnection(ucb.UserConnection.ToRaw())

// Write to DB
if _, err := r.Ctx.Inst().Mongo.Collection(mongo.CollectionNameUsers).UpdateOne(ctx, bson.M{
"_id": actor.ID,
}, ub.Update); err != nil {
zap.S().Errorw("mongo, failed to write update of user verifying their youtube channel",
"error", err,
)

return errors.ErrInternalServerError()
}

// Clear redis
_, _ = r.Ctx.Inst().Redis.Del(ctx, rkey)

return ctx.JSON(rest.OK, verifyResult{
Channel: channel,
ChannelID: channel.Id,
Verified: true,
UserID: actor.ID.Hex(),
})
}

type verifyResult struct {
Channel *youtube.Channel `json:"channel"`
ChannelID string `json:"channel_id"`
Verified bool `json:"verified"`
UserID string `json:"user_id"`
}
2 changes: 1 addition & 1 deletion internal/rest/v2/routes/user/user.emotes.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (r *emotes) Handler(ctx *rest.Ctx) errors.APIError {
}

filter := utils.Ternary(id.IsZero(), bson.M{"$or": bson.A{
bson.M{"connections.id": strings.ToLower(key)},
bson.M{"connections.id": key},
bson.M{"username": strings.ToLower(key)},
}}, bson.M{
"_id": id,
Expand Down
Loading