mirror of
https://git.vectorsigma.ru/public/photoview.git
synced 2026-08-03 21:09:05 +00:00
Detect if share token is password protected
This commit is contained in:
@@ -137,15 +137,16 @@ type ComplexityRoot struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Query struct {
|
Query struct {
|
||||||
Album func(childComplexity int, id int) int
|
Album func(childComplexity int, id int) int
|
||||||
MyAlbums func(childComplexity int, filter *models.Filter, onlyRoot *bool, showEmpty *bool) int
|
MyAlbums func(childComplexity int, filter *models.Filter, onlyRoot *bool, showEmpty *bool) int
|
||||||
MyPhotos func(childComplexity int, filter *models.Filter) int
|
MyPhotos func(childComplexity int, filter *models.Filter) int
|
||||||
MyUser func(childComplexity int) int
|
MyUser func(childComplexity int) int
|
||||||
Photo func(childComplexity int, id int) int
|
Photo func(childComplexity int, id int) int
|
||||||
Search func(childComplexity int, query string, limitPhotos *int, limitAlbums *int) int
|
Search func(childComplexity int, query string, limitPhotos *int, limitAlbums *int) int
|
||||||
ShareToken func(childComplexity int, token string, password *string) int
|
ShareToken func(childComplexity int, token string, password *string) int
|
||||||
SiteInfo func(childComplexity int) int
|
ShareTokenRequiresPassword func(childComplexity int, token string) int
|
||||||
User func(childComplexity int, filter *models.Filter) int
|
SiteInfo func(childComplexity int) int
|
||||||
|
User func(childComplexity int, filter *models.Filter) int
|
||||||
}
|
}
|
||||||
|
|
||||||
ScannerResult struct {
|
ScannerResult struct {
|
||||||
@@ -228,6 +229,7 @@ type QueryResolver interface {
|
|||||||
MyPhotos(ctx context.Context, filter *models.Filter) ([]*models.Photo, error)
|
MyPhotos(ctx context.Context, filter *models.Filter) ([]*models.Photo, error)
|
||||||
Photo(ctx context.Context, id int) (*models.Photo, error)
|
Photo(ctx context.Context, id int) (*models.Photo, error)
|
||||||
ShareToken(ctx context.Context, token string, password *string) (*models.ShareToken, error)
|
ShareToken(ctx context.Context, token string, password *string) (*models.ShareToken, error)
|
||||||
|
ShareTokenRequiresPassword(ctx context.Context, token string) (bool, error)
|
||||||
Search(ctx context.Context, query string, limitPhotos *int, limitAlbums *int) (*models.SearchResult, error)
|
Search(ctx context.Context, query string, limitPhotos *int, limitAlbums *int) (*models.SearchResult, error)
|
||||||
}
|
}
|
||||||
type ShareTokenResolver interface {
|
type ShareTokenResolver interface {
|
||||||
@@ -827,6 +829,18 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
|
|||||||
|
|
||||||
return e.complexity.Query.ShareToken(childComplexity, args["token"].(string), args["password"].(*string)), true
|
return e.complexity.Query.ShareToken(childComplexity, args["token"].(string), args["password"].(*string)), true
|
||||||
|
|
||||||
|
case "Query.shareTokenRequiresPassword":
|
||||||
|
if e.complexity.Query.ShareTokenRequiresPassword == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
args, err := ec.field_Query_shareTokenRequiresPassword_args(context.TODO(), rawArgs)
|
||||||
|
if err != nil {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
return e.complexity.Query.ShareTokenRequiresPassword(childComplexity, args["token"].(string)), true
|
||||||
|
|
||||||
case "Query.siteInfo":
|
case "Query.siteInfo":
|
||||||
if e.complexity.Query.SiteInfo == nil {
|
if e.complexity.Query.SiteInfo == nil {
|
||||||
break
|
break
|
||||||
@@ -1108,6 +1122,7 @@ type Query {
|
|||||||
photo(id: Int!): Photo!
|
photo(id: Int!): Photo!
|
||||||
|
|
||||||
shareToken(token: String!, password: String): ShareToken!
|
shareToken(token: String!, password: String): ShareToken!
|
||||||
|
shareTokenRequiresPassword(token: String!): Boolean!
|
||||||
|
|
||||||
search(query: String!, limitPhotos: Int, limitAlbums: Int): SearchResult!
|
search(query: String!, limitPhotos: Int, limitAlbums: Int): SearchResult!
|
||||||
}
|
}
|
||||||
@@ -1752,6 +1767,20 @@ func (ec *executionContext) field_Query_search_args(ctx context.Context, rawArgs
|
|||||||
return args, nil
|
return args, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ec *executionContext) field_Query_shareTokenRequiresPassword_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) {
|
||||||
|
var err error
|
||||||
|
args := map[string]interface{}{}
|
||||||
|
var arg0 string
|
||||||
|
if tmp, ok := rawArgs["token"]; ok {
|
||||||
|
arg0, err = ec.unmarshalNString2string(ctx, tmp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
args["token"] = arg0
|
||||||
|
return args, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (ec *executionContext) field_Query_shareToken_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) {
|
func (ec *executionContext) field_Query_shareToken_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) {
|
||||||
var err error
|
var err error
|
||||||
args := map[string]interface{}{}
|
args := map[string]interface{}{}
|
||||||
@@ -4325,6 +4354,47 @@ func (ec *executionContext) _Query_shareToken(ctx context.Context, field graphql
|
|||||||
return ec.marshalNShareToken2ᚖgithubᚗcomᚋviktorstrateᚋphotoviewᚋapiᚋgraphqlᚋmodelsᚐShareToken(ctx, field.Selections, res)
|
return ec.marshalNShareToken2ᚖgithubᚗcomᚋviktorstrateᚋphotoviewᚋapiᚋgraphqlᚋmodelsᚐShareToken(ctx, field.Selections, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ec *executionContext) _Query_shareTokenRequiresPassword(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
ec.Error(ctx, ec.Recover(ctx, r))
|
||||||
|
ret = graphql.Null
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
fc := &graphql.FieldContext{
|
||||||
|
Object: "Query",
|
||||||
|
Field: field,
|
||||||
|
Args: nil,
|
||||||
|
IsMethod: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx = graphql.WithFieldContext(ctx, fc)
|
||||||
|
rawArgs := field.ArgumentMap(ec.Variables)
|
||||||
|
args, err := ec.field_Query_shareTokenRequiresPassword_args(ctx, rawArgs)
|
||||||
|
if err != nil {
|
||||||
|
ec.Error(ctx, err)
|
||||||
|
return graphql.Null
|
||||||
|
}
|
||||||
|
fc.Args = args
|
||||||
|
resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) {
|
||||||
|
ctx = rctx // use context from middleware stack in children
|
||||||
|
return ec.resolvers.Query().ShareTokenRequiresPassword(rctx, args["token"].(string))
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
ec.Error(ctx, err)
|
||||||
|
return graphql.Null
|
||||||
|
}
|
||||||
|
if resTmp == nil {
|
||||||
|
if !graphql.HasFieldError(ctx, fc) {
|
||||||
|
ec.Errorf(ctx, "must not be null")
|
||||||
|
}
|
||||||
|
return graphql.Null
|
||||||
|
}
|
||||||
|
res := resTmp.(bool)
|
||||||
|
fc.Result = res
|
||||||
|
return ec.marshalNBoolean2bool(ctx, field.Selections, res)
|
||||||
|
}
|
||||||
|
|
||||||
func (ec *executionContext) _Query_search(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
|
func (ec *executionContext) _Query_search(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
@@ -6880,6 +6950,20 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr
|
|||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
})
|
})
|
||||||
|
case "shareTokenRequiresPassword":
|
||||||
|
field := field
|
||||||
|
out.Concurrently(i, func() (res graphql.Marshaler) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
ec.Error(ctx, ec.Recover(ctx, r))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
res = ec._Query_shareTokenRequiresPassword(ctx, field)
|
||||||
|
if res == graphql.Null {
|
||||||
|
atomic.AddUint32(&invalids, 1)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
})
|
||||||
case "search":
|
case "search":
|
||||||
field := field
|
field := field
|
||||||
out.Concurrently(i, func() (res graphql.Marshaler) {
|
out.Concurrently(i, func() (res graphql.Marshaler) {
|
||||||
|
|||||||
@@ -84,6 +84,21 @@ func (r *queryResolver) ShareToken(ctx context.Context, tokenValue string, passw
|
|||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *queryResolver) ShareTokenRequiresPassword(ctx context.Context, tokenValue string) (bool, error) {
|
||||||
|
row := r.Database.QueryRow("SELECT * FROM share_token WHERE value = ?", tokenValue)
|
||||||
|
token, err := models.NewShareTokenFromRow(row)
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return false, errors.New("share not found")
|
||||||
|
} else {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
requiresPassword := token.Password != nil
|
||||||
|
return requiresPassword, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *mutationResolver) ShareAlbum(ctx context.Context, albumID int, expire *time.Time, password *string) (*models.ShareToken, error) {
|
func (r *mutationResolver) ShareAlbum(ctx context.Context, albumID int, expire *time.Time, password *string) (*models.ShareToken, error) {
|
||||||
user := auth.UserFromContext(ctx)
|
user := auth.UserFromContext(ctx)
|
||||||
if user == nil {
|
if user == nil {
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ type Query {
|
|||||||
photo(id: Int!): Photo!
|
photo(id: Int!): Photo!
|
||||||
|
|
||||||
shareToken(token: String!, password: String): ShareToken!
|
shareToken(token: String!, password: String): ShareToken!
|
||||||
|
shareTokenRequiresPassword(token: String!): Boolean!
|
||||||
|
|
||||||
search(query: String!, limitPhotos: Int, limitAlbums: Int): SearchResult!
|
search(query: String!, limitPhotos: Int, limitAlbums: Int): SearchResult!
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
import PropTypes from 'prop-types'
|
||||||
import RouterProps from 'react-router-prop-types'
|
import RouterProps from 'react-router-prop-types'
|
||||||
import { Route, Switch } from 'react-router-dom'
|
import { Route, Switch } from 'react-router-dom'
|
||||||
import AlbumSharePage from './AlbumSharePage'
|
import AlbumSharePage from './AlbumSharePage'
|
||||||
import PhotoSharePage from './PhotoSharePage'
|
import PhotoSharePage from './PhotoSharePage'
|
||||||
import { Query } from 'react-apollo'
|
import { useQuery } from 'react-apollo'
|
||||||
import gql from 'graphql-tag'
|
import gql from 'graphql-tag'
|
||||||
|
|
||||||
const tokenQuery = gql`
|
const tokenQuery = gql`
|
||||||
query SharePageToken($token: String!) {
|
query SharePageToken($token: String!, $password: String) {
|
||||||
shareToken(token: $token) {
|
shareToken(token: $token, password: $password) {
|
||||||
token
|
token
|
||||||
album {
|
album {
|
||||||
...AlbumProps
|
...AlbumProps
|
||||||
@@ -70,30 +71,60 @@ const tokenQuery = gql`
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const tokenPasswordProtectedQuery = gql`
|
||||||
|
query ShareTokenRequiresPassword($token: String!) {
|
||||||
|
shareTokenRequiresPassword(token: $token)
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
const AuthorizedTokenRoute = ({ match, password }) => {
|
||||||
|
const { loading, error, data } = useQuery(tokenQuery, {
|
||||||
|
variables: { token: match.params.token, password },
|
||||||
|
})
|
||||||
|
|
||||||
|
if (error) return error.message
|
||||||
|
if (loading) return 'Loading...'
|
||||||
|
|
||||||
|
if (data.shareToken.album) {
|
||||||
|
return <AlbumSharePage album={data.shareToken.album} match={match} />
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.shareToken.photo) {
|
||||||
|
return <PhotoSharePage photo={data.shareToken.photo} />
|
||||||
|
}
|
||||||
|
|
||||||
|
return <h1>Share not found</h1>
|
||||||
|
}
|
||||||
|
|
||||||
|
AuthorizedTokenRoute.propTypes = {
|
||||||
|
match: PropTypes.object.isRequired,
|
||||||
|
password: PropTypes.string,
|
||||||
|
}
|
||||||
|
|
||||||
|
const TokenRoute = ({ match }) => {
|
||||||
|
const { loading, error, data } = useQuery(tokenPasswordProtectedQuery, {
|
||||||
|
variables: { token: match.params.token },
|
||||||
|
})
|
||||||
|
|
||||||
|
if (error) return error.message
|
||||||
|
if (loading) return 'Loading...'
|
||||||
|
|
||||||
|
if (data.shareTokenRequiresPassword == true) {
|
||||||
|
return 'Please provide password'
|
||||||
|
}
|
||||||
|
|
||||||
|
return <AuthorizedTokenRoute match={match} />
|
||||||
|
}
|
||||||
|
|
||||||
|
TokenRoute.propTypes = {
|
||||||
|
match: PropTypes.object.isRequired,
|
||||||
|
}
|
||||||
|
|
||||||
const SharePage = ({ match }) => {
|
const SharePage = ({ match }) => {
|
||||||
return (
|
return (
|
||||||
<Switch>
|
<Switch>
|
||||||
<Route path={`${match.url}/:token`}>
|
<Route path={`${match.url}/:token`}>
|
||||||
{({ match }) => (
|
{({ match }) => <TokenRoute match={match} />}
|
||||||
<Query query={tokenQuery} variables={{ token: match.params.token }}>
|
|
||||||
{({ loading, error, data }) => {
|
|
||||||
if (error) return error.message
|
|
||||||
if (loading) return 'Loading...'
|
|
||||||
|
|
||||||
if (data.shareToken.album) {
|
|
||||||
return (
|
|
||||||
<AlbumSharePage album={data.shareToken.album} match={match} />
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.shareToken.photo) {
|
|
||||||
return <PhotoSharePage photo={data.shareToken.photo} />
|
|
||||||
}
|
|
||||||
|
|
||||||
return <h1>Share not found</h1>
|
|
||||||
}}
|
|
||||||
</Query>
|
|
||||||
)}
|
|
||||||
</Route>
|
</Route>
|
||||||
<Route path="/">Share not found</Route>
|
<Route path="/">Share not found</Route>
|
||||||
</Switch>
|
</Switch>
|
||||||
|
|||||||
Reference in New Issue
Block a user