Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the HereSphere API #3794

Closed
wants to merge 160 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
160 commits
Select commit Hold shift + click to select a range
a4880d1
Initial api attempt (non-functional)
NodudeWasTaken May 31, 2023
8e2250c
HereSphere API now works
NodudeWasTaken Jun 1, 2023
1b79014
Implemented various features in the heresphere api
NodudeWasTaken Jun 1, 2023
df1ad14
Update to follow spec more closely
NodudeWasTaken Jun 1, 2023
28604d9
Added absolute path to captions
NodudeWasTaken Jun 1, 2023
cd6a48a
Added get multiple tags
NodudeWasTaken Jun 2, 2023
699033e
Working authentication
NodudeWasTaken Jun 2, 2023
a784f6c
Added vr projection detection
NodudeWasTaken Jun 2, 2023
50a172f
The linting update
NodudeWasTaken Jun 2, 2023
ff4d262
Add redirect for heresphere
NodudeWasTaken Jun 2, 2023
a24ab8f
Event server implemented (for timestamp use only)
NodudeWasTaken Jun 2, 2023
14a1743
Auth for events
NodudeWasTaken Jun 2, 2023
8355dcd
Make auth nicer
NodudeWasTaken Jun 2, 2023
c86dac7
Defeat the linter
NodudeWasTaken Jun 2, 2023
e5d41d1
Make the linter even happier
NodudeWasTaken Jun 2, 2023
8de4cc6
Small update
NodudeWasTaken Jun 2, 2023
3a1ec26
Fixed redirect
NodudeWasTaken Jun 3, 2023
5f46303
Joined FindProjection functions
NodudeWasTaken Jun 3, 2023
0b2c4c2
Use urlbuilder
NodudeWasTaken Jun 4, 2023
f5f386f
/scan is now faster (43s -> 8s)
NodudeWasTaken Jun 4, 2023
2e8a33e
Forgot an assignment
NodudeWasTaken Jun 4, 2023
7c17101
Transcoding
NodudeWasTaken Jun 5, 2023
f770c29
Dont pointer for no reason
NodudeWasTaken Jun 5, 2023
77f6699
Transcode check right potential zero value
NodudeWasTaken Jun 5, 2023
f0b166c
/scan is fast enough to work, but not to make stash not 500 SLOW SQL
NodudeWasTaken Jun 5, 2023
2f5ffb2
Bigass locks and scan go bye
NodudeWasTaken Jun 5, 2023
86dd3e1
Added TODO on event
NodudeWasTaken Jun 5, 2023
fcae04a
Fix lint and add float rating conversion
NodudeWasTaken Jun 5, 2023
5a705c9
Cleanup
NodudeWasTaken Jun 5, 2023
0ea4136
Fix misspell
NodudeWasTaken Jun 5, 2023
78700bd
Add missing
NodudeWasTaken Jun 5, 2023
f89aff9
Nicer apikey add
NodudeWasTaken Jun 5, 2023
dfbf499
Unecessary
NodudeWasTaken Jun 5, 2023
99839a8
Fix remaining heresphere bugs
NodudeWasTaken Jun 5, 2023
1056b87
Changed redirect to just a optional button
NodudeWasTaken Jun 5, 2023
db2ee3e
Make linter happy
NodudeWasTaken Jun 5, 2023
f3ef04d
Aling button
NodudeWasTaken Jun 5, 2023
dc9aa3f
Proper fov detect
NodudeWasTaken Jun 5, 2023
193f726
The linters happiness is the only cure
NodudeWasTaken Jun 5, 2023
5e0130a
effective assign
NodudeWasTaken Jun 5, 2023
5163b60
Add watched/unwatched tag
NodudeWasTaken Jun 6, 2023
f83014b
Linter'b'happy
NodudeWasTaken Jun 6, 2023
7ed7948
Fix integer division
NodudeWasTaken Jun 6, 2023
4e54775
Add missing unused struct
NodudeWasTaken Jun 6, 2023
21d54f8
Tags WIP
NodudeWasTaken Jun 7, 2023
c3fd77a
Tags broken WIP
NodudeWasTaken Jun 7, 2023
ff66a15
Alright, tags are broken idk why
NodudeWasTaken Jun 7, 2023
53a0ffd
Working tags
NodudeWasTaken Jun 9, 2023
0c4774f
Favorites not working
NodudeWasTaken Jun 9, 2023
57c980d
Make it a tiny bit nicer
NodudeWasTaken Jun 10, 2023
9ad1804
Merge branch 'develop' of https://github.com/stashapp/stash into here…
NodudeWasTaken Jun 10, 2023
8ec46f4
Actually try writing favorites
NodudeWasTaken Jun 10, 2023
554f9a4
Heatmap'n'other stuff
NodudeWasTaken Jun 11, 2023
9985cf9
Fix heatmap draw
NodudeWasTaken Jun 11, 2023
1803b1b
Fix heatmap 2
NodudeWasTaken Jun 11, 2023
ce75ef9
Add overlay var
NodudeWasTaken Jun 11, 2023
b57908b
Removed thumbnail
NodudeWasTaken Jun 12, 2023
0806c3a
PlayCount, OCounter, comments, tags, better errors, more
NodudeWasTaken Jun 23, 2023
a7402b8
Merge branch 'develop' of https://github.com/stashapp/stash into here…
NodudeWasTaken Jun 23, 2023
f236318
Fix conflicts
NodudeWasTaken Jun 23, 2023
cee749e
Standardization
NodudeWasTaken Jul 2, 2023
d57ac82
Revert "Fix heatmap 2"
NodudeWasTaken Jul 2, 2023
6b7f849
Revert "Fix heatmap draw"
NodudeWasTaken Jul 2, 2023
89d180d
Remove comment about vrtag
NodudeWasTaken Jul 2, 2023
0b8ca43
Remove ambiguity in tag detection
NodudeWasTaken Jul 2, 2023
c623f0a
Remove some comments
NodudeWasTaken Jul 2, 2023
67457e2
Apply linter'b'happy
NodudeWasTaken Jul 2, 2023
8433f2e
Better tags
NodudeWasTaken Jul 6, 2023
c7971ee
Notes
NodudeWasTaken Jul 11, 2023
de5f932
PlayCount auto add
NodudeWasTaken Jul 11, 2023
ad88466
GO 1.19 compatible
NodudeWasTaken Jul 11, 2023
5a04b6c
Merge branch 'develop' of https://github.com/stashapp/stash into here…
NodudeWasTaken Jul 11, 2023
aeab6b3
Marker add
NodudeWasTaken Jul 11, 2023
cd15489
Add favorite tag back (not fully implemented yet)
NodudeWasTaken Jul 11, 2023
df24dbd
Fix incorrect scene value set
NodudeWasTaken Jul 11, 2023
d29293c
Add favorite find
NodudeWasTaken Jul 11, 2023
293aff8
Updated tag/performer/marker creation to work in heresphere (and hope…
NodudeWasTaken Jul 13, 2023
90c895f
Merge branch 'develop' of https://github.com/stashapp/stash into here…
NodudeWasTaken Jul 13, 2023
04f3e87
Linter magic
NodudeWasTaken Jul 13, 2023
4d0ee31
Disable non-working performer and tag creation
NodudeWasTaken Jul 13, 2023
c91150c
Ignore some errors
NodudeWasTaken Jul 13, 2023
02089a1
Linter and json fixes
NodudeWasTaken Jul 13, 2023
c23b759
Merge branch 'develop' of https://github.com/stashapp/stash into here…
NodudeWasTaken Jul 14, 2023
d48fdd3
HereSphere enabled (missing ui)
NodudeWasTaken Jul 17, 2023
271d464
Marker scheme
NodudeWasTaken Jul 17, 2023
f9771f8
Merge branch 'develop' of https://github.com/stashapp/stash into here…
NodudeWasTaken Jul 31, 2023
6555504
Merge branch 'develop' of https://github.com/stashapp/stash into here…
NodudeWasTaken Aug 3, 2023
bd0d87d
Quick update
NodudeWasTaken Aug 3, 2023
57374e1
Captions work
NodudeWasTaken Aug 3, 2023
85ac875
Im an idiot
NodudeWasTaken Aug 3, 2023
496de9c
Niceify
NodudeWasTaken Aug 3, 2023
ac415d4
Remove heresphere button
NodudeWasTaken Aug 8, 2023
9ddff62
No more heresphere button 2
NodudeWasTaken Aug 8, 2023
724c164
HSP Settings in Services section
NodudeWasTaken Aug 8, 2023
bbdaa17
Small ui fixes
NodudeWasTaken Aug 8, 2023
5ca7cf9
Formatting
NodudeWasTaken Aug 8, 2023
8065205
Fix datebug
NodudeWasTaken Aug 8, 2023
fa88270
Rating as tag
NodudeWasTaken Aug 8, 2023
792a02e
Rename
NodudeWasTaken Aug 8, 2023
b1fae4b
Remove wrong comment
NodudeWasTaken Aug 8, 2023
024a5ba
Proper set
NodudeWasTaken Aug 8, 2023
8c6031a
Comment
NodudeWasTaken Aug 8, 2023
498b2df
Fixed favorites call
NodudeWasTaken Aug 8, 2023
a8c3ebc
Small comment
NodudeWasTaken Aug 8, 2023
156bdff
Smol comment
NodudeWasTaken Aug 8, 2023
285fbef
Small interface update
NodudeWasTaken Aug 8, 2023
e408aee
Proper subheading
NodudeWasTaken Aug 8, 2023
e941de9
Prettier
NodudeWasTaken Aug 8, 2023
504bc85
Add proper error check to interactive tag
NodudeWasTaken Aug 8, 2023
99ae36e
Small translation update
NodudeWasTaken Aug 9, 2023
1cc55d6
Set FOV when tag found
NodudeWasTaken Aug 9, 2023
bf71d52
Mistake in marker handling, might need a closer look
NodudeWasTaken Aug 10, 2023
04567a6
UI heresphere favorite tag is now a dropdown
NodudeWasTaken Aug 13, 2023
ce219a9
UI Linter wants "key" in options
NodudeWasTaken Aug 13, 2023
ff81920
UI Do tag select in a little bit nice way
NodudeWasTaken Aug 14, 2023
7086d0f
UI Bring tag select into compliance with the rest of the UI
NodudeWasTaken Aug 14, 2023
5c7ac7f
UI Linter happy
NodudeWasTaken Aug 14, 2023
8555c43
Merge branch 'develop' of https://github.com/stashapp/stash into here…
NodudeWasTaken Aug 21, 2023
9b36ded
Remove unused graphql
NodudeWasTaken Aug 28, 2023
47a7eaa
Initial refactor
NodudeWasTaken Aug 30, 2023
0a4b2df
Bugfixes and logging
NodudeWasTaken Aug 30, 2023
2f018fc
Moved playback event check
NodudeWasTaken Aug 30, 2023
de77dde
Only add funspeed if interactive
NodudeWasTaken Aug 30, 2023
b4cdf3e
Fix missing pointer for tag updating
NodudeWasTaken Aug 30, 2023
2082909
2nd round of refactors
NodudeWasTaken Aug 31, 2023
ba7fd1e
More tag refactor + marker bugfix
NodudeWasTaken Aug 31, 2023
2bf88b9
Might aswell use LoginPlain
NodudeWasTaken Aug 31, 2023
e0cee50
Merge branch 'develop' of https://github.com/stashapp/stash into here…
NodudeWasTaken Sep 1, 2023
6513e33
Update for model refactor
NodudeWasTaken Sep 1, 2023
81a9275
Fix linter error
NodudeWasTaken Sep 1, 2023
e3b70d1
Missed one
NodudeWasTaken Sep 1, 2023
a58df70
Add filter parsing to HSP API
NodudeWasTaken Sep 3, 2023
bbe4d02
Split up tags even more
NodudeWasTaken Sep 6, 2023
a953480
Small change
NodudeWasTaken Sep 6, 2023
72d9c8a
Merge branch 'develop' of https://github.com/stashapp/stash into here…
NodudeWasTaken Sep 11, 2023
7efdd54
Fix playcount tracker
NodudeWasTaken Sep 11, 2023
45c5d17
Sort filter names
NodudeWasTaken Sep 11, 2023
c4fe002
Fix indent
NodudeWasTaken Sep 12, 2023
0dbe5f2
Merge branch 'develop' of https://github.com/stashapp/stash into here…
NodudeWasTaken Sep 12, 2023
0b98b46
Temporal storage for playcount
NodudeWasTaken Sep 12, 2023
60c3fc5
Fix temoral storage var
NodudeWasTaken Sep 12, 2023
cef4725
Small tags update
NodudeWasTaken Sep 16, 2023
1e53c08
Small logic fix from prev commit
NodudeWasTaken Sep 16, 2023
e747272
Detect skip events when adding duration
NodudeWasTaken Sep 20, 2023
4298fd4
Fixed bug where favoriting would delete tags
NodudeWasTaken Oct 10, 2023
2287dd6
Fixed MovieIDs == nil bug
NodudeWasTaken Oct 10, 2023
e68ee8a
Fix small logic error in tags_write
NodudeWasTaken Oct 17, 2023
80168a0
Merge https://github.com/stashapp/stash into heresphere-api
NodudeWasTaken Oct 17, 2023
d6f6fe9
Compliance update
NodudeWasTaken Oct 18, 2023
085af70
Merge https://github.com/stashapp/stash into heresphere-api
NodudeWasTaken Oct 18, 2023
448a51e
Bugfix from prev
NodudeWasTaken Oct 18, 2023
194f8cb
prettier
NodudeWasTaken Oct 18, 2023
5941392
Proper scene deletion
NodudeWasTaken Oct 31, 2023
1c5a88e
Linter fix
NodudeWasTaken Oct 31, 2023
3189a3b
Merge branch 'develop' of https://github.com/stashapp/stash into here…
NodudeWasTaken Nov 22, 2023
fbb2d81
Fix relationships.go and chi in hsp routes.go
NodudeWasTaken Nov 23, 2023
f6fa975
Some ui context.tsx change
NodudeWasTaken Nov 23, 2023
2aefe6b
Merge branch 'develop' of https://github.com/stashapp/stash into here…
NodudeWasTaken Nov 28, 2023
b2dbb29
Also use default filter
NodudeWasTaken Nov 30, 2023
5838238
Merge branch 'develop' of https://github.com/stashapp/stash into here…
NodudeWasTaken Dec 26, 2023
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
12 changes: 12 additions & 0 deletions graphql/documents/data/config.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ fragment ConfigDLNAData on ConfigDLNAResult {
videoSortOrder
}

fragment ConfigHSPData on ConfigHSPResult {
enabled
favoriteTagId
writeFavorites
writeRatings
writeTags
writeDeletes
}

fragment ConfigScrapingData on ConfigScrapingResult {
scraperUserAgent
scraperCertCheck
Expand Down Expand Up @@ -212,6 +221,9 @@ fragment ConfigData on ConfigResult {
dlna {
...ConfigDLNAData
}
hsp {
...ConfigHSPData
}
scraping {
...ConfigScrapingData
}
Expand Down
6 changes: 6 additions & 0 deletions graphql/documents/mutations/config.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ mutation ConfigureDLNA($input: ConfigDLNAInput!) {
}
}

mutation ConfigureHSP($input: ConfigHSPInput!) {
configureHSP(input: $input) {
...ConfigHSPData
}
}

mutation ConfigureScraping($input: ConfigScrapingInput!) {
configureScraping(input: $input) {
...ConfigScrapingData
Expand Down
1 change: 1 addition & 0 deletions graphql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ type Mutation {
configureGeneral(input: ConfigGeneralInput!): ConfigGeneralResult!
configureInterface(input: ConfigInterfaceInput!): ConfigInterfaceResult!
configureDLNA(input: ConfigDLNAInput!): ConfigDLNAResult!
configureHSP(input: ConfigHSPInput!): ConfigHSPResult!
configureScraping(input: ConfigScrapingInput!): ConfigScrapingResult!
configureDefaults(
input: ConfigDefaultSettingsInput!
Expand Down
31 changes: 31 additions & 0 deletions graphql/schema/types/config.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,36 @@ type ConfigDLNAResult {
videoSortOrder: String!
}

input ConfigHSPInput {
"True if HSP Api should be enabled by default"
enabled: Boolean
"ID of the favorite tag"
favoriteTagId: Int
"True if writing favorites to HSP Api should be enabled"
writeFavorites: Boolean
"True if writing ratings to HSP Api should be enabled"
writeRatings: Boolean
"True if writing tags to HSP Api should be enabled"
writeTags: Boolean
"True if writing deletes to HSP Api should be enabled"
writeDeletes: Boolean
}

type ConfigHSPResult {
"True if HSP Api should be enabled by default"
enabled: Boolean!
"ID of the favorite tag"
favoriteTagId: Int!
"True if writing favorites to HSP Api should be enabled"
writeFavorites: Boolean!
"True if writing ratings to HSP Api should be enabled"
writeRatings: Boolean!
"True if writing tags to HSP Api should be enabled"
writeTags: Boolean!
"True if writing deletes to HSP Api should be enabled"
writeDeletes: Boolean!
}

input ConfigScrapingInput {
"Scraper user agent string"
scraperUserAgent: String
Expand Down Expand Up @@ -532,6 +562,7 @@ type ConfigResult {
general: ConfigGeneralResult!
interface: ConfigInterfaceResult!
dlna: ConfigDLNAResult!
hsp: ConfigHSPResult!
scraping: ConfigScrapingResult!
defaults: ConfigDefaultSettingsResult!
ui: Map!
Expand Down
4 changes: 2 additions & 2 deletions internal/api/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (

func allowUnauthenticated(r *http.Request) bool {
// #2715 - allow access to UI files
return strings.HasPrefix(r.URL.Path, loginEndpoint) || r.URL.Path == logoutEndpoint || r.URL.Path == "/css" || strings.HasPrefix(r.URL.Path, "/assets")
return strings.HasPrefix(r.URL.Path, loginEndpoint) || r.URL.Path == logoutEndpoint || r.URL.Path == "/css" || strings.HasPrefix(r.URL.Path, "/assets") || strings.HasPrefix(r.URL.Path, "/heresphere")
}

func authenticateHandler() func(http.Handler) http.Handler {
Expand Down Expand Up @@ -84,7 +84,7 @@ func authenticateHandler() func(http.Handler) http.Handler {
return
}

prefix := getProxyPrefix(r)
prefix := manager.GetProxyPrefix(r)

// otherwise redirect to the login page
returnURL := url.URL{
Expand Down
29 changes: 29 additions & 0 deletions internal/api/resolver_mutation_configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,35 @@ func (r *mutationResolver) ConfigureDlna(ctx context.Context, input ConfigDLNAIn
return makeConfigDLNAResult(), nil
}

func (r *mutationResolver) ConfigureHsp(ctx context.Context, input ConfigHSPInput) (*ConfigHSPResult, error) {
c := config.GetInstance()

if input.Enabled != nil {
c.Set(config.HSPDefaultEnabled, *input.Enabled)
}
if input.FavoriteTagID != nil {
c.Set(config.HSPFavoriteTag, *input.FavoriteTagID)
}
if input.WriteFavorites != nil {
c.Set(config.HSPWriteFavorites, *input.WriteFavorites)
}
if input.WriteRatings != nil {
c.Set(config.HSPWriteRating, *input.WriteRatings)
}
if input.WriteTags != nil {
c.Set(config.HSPWriteTags, *input.WriteTags)
}
if input.WriteDeletes != nil {
c.Set(config.HSPWriteDeletes, *input.WriteDeletes)
}

if err := c.Write(); err != nil {
return makeConfigHSPResult(), err
}

return makeConfigHSPResult(), nil
}

func (r *mutationResolver) ConfigureScraping(ctx context.Context, input ConfigScrapingInput) (*ConfigScrapingResult, error) {
c := config.GetInstance()

Expand Down
14 changes: 14 additions & 0 deletions internal/api/resolver_query_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func makeConfigResult() *ConfigResult {
General: makeConfigGeneralResult(),
Interface: makeConfigInterfaceResult(),
Dlna: makeConfigDLNAResult(),
Hsp: makeConfigHSPResult(),
Scraping: makeConfigScrapingResult(),
Defaults: makeConfigDefaultsResult(),
UI: makeConfigUIResult(),
Expand Down Expand Up @@ -203,6 +204,19 @@ func makeConfigDLNAResult() *ConfigDLNAResult {
}
}

func makeConfigHSPResult() *ConfigHSPResult {
config := config.GetInstance()

return &ConfigHSPResult{
Enabled: config.GetHSPDefaultEnabled(),
FavoriteTagID: config.GetHSPFavoriteTag(),
WriteFavorites: config.GetHSPWriteFavorites(),
WriteRatings: config.GetHSPWriteRatings(),
WriteTags: config.GetHSPWriteTags(),
WriteDeletes: config.GetHSPWriteDeletes(),
}
}

func makeConfigScrapingResult() *ConfigScrapingResult {
config := config.GetInstance()

Expand Down
29 changes: 10 additions & 19 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/stashapp/stash/internal/api/loaders"
"github.com/stashapp/stash/internal/build"
"github.com/stashapp/stash/internal/heresphere"
"github.com/stashapp/stash/internal/manager"
"github.com/stashapp/stash/internal/manager/config"
"github.com/stashapp/stash/pkg/fsutil"
Expand Down Expand Up @@ -179,7 +180,7 @@ func Initialize() (*Server, error) {
r.HandleFunc(gqlEndpoint, gqlHandlerFunc)
r.HandleFunc(playgroundEndpoint, func(w http.ResponseWriter, r *http.Request) {
setPageSecurityHeaders(w, r, pluginCache.ListPlugins())
endpoint := getProxyPrefix(r) + gqlEndpoint
endpoint := manager.GetProxyPrefix(r) + gqlEndpoint
gqlPlayground.Handler("GraphQL playground", endpoint)(w, r)
})

Expand All @@ -191,6 +192,7 @@ func Initialize() (*Server, error) {
r.Mount("/tag", server.getTagRoutes())
r.Mount("/downloads", server.getDownloadsRoutes())
r.Mount("/plugin", server.getPluginRoutes())
r.Mount("/heresphere", server.getHeresphereRoutes())

r.HandleFunc("/css", cssHandler(cfg))
r.HandleFunc("/javascript", javascriptHandler(cfg))
Expand Down Expand Up @@ -237,7 +239,7 @@ func Initialize() (*Server, error) {
}
indexHtml := string(data)

prefix := getProxyPrefix(r)
prefix := manager.GetProxyPrefix(r)
indexHtml = strings.ReplaceAll(indexHtml, "%COLOR%", themeColor)
indexHtml = strings.Replace(indexHtml, `<base href="/"`, fmt.Sprintf(`<base href="%s/"`, prefix), 1)

Expand Down Expand Up @@ -344,6 +346,11 @@ func (s *Server) getPluginRoutes() chi.Router {
}.Routes()
}

func (s *Server) getHeresphereRoutes() chi.Router {
repo := s.manager.Repository
return heresphere.GetRoutes(repo)
}

func copyFile(w io.Writer, path string) error {
f, err := os.Open(path)
if err != nil {
Expand Down Expand Up @@ -568,26 +575,10 @@ func BaseURLMiddleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

scheme := "http"
if strings.Compare("https", r.URL.Scheme) == 0 || r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" {
scheme = "https"
}
prefix := getProxyPrefix(r)

baseURL := scheme + "://" + r.Host + prefix

externalHost := config.GetInstance().GetExternalHost()
if externalHost != "" {
baseURL = externalHost + prefix
}

baseURL := manager.GetBaseURL(r)
r = r.WithContext(context.WithValue(ctx, BaseURLCtxKey, baseURL))

next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}

func getProxyPrefix(r *http.Request) string {
return strings.TrimRight(r.Header.Get("X-Forwarded-Prefix"), "/")
}
8 changes: 4 additions & 4 deletions internal/api/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type loginTemplateData struct {

func serveLoginPage(w http.ResponseWriter, r *http.Request, returnURL string, loginError string) {
loginPage := string(getLoginPage())
prefix := getProxyPrefix(r)
prefix := manager.GetProxyPrefix(r)
loginPage = strings.ReplaceAll(loginPage, "/%BASE_URL%", prefix)

templ, err := template.New("Login").Parse(loginPage)
Expand Down Expand Up @@ -66,7 +66,7 @@ func handleLogin() http.HandlerFunc {
if returnURL != "" {
http.Redirect(w, r, returnURL, http.StatusFound)
} else {
prefix := getProxyPrefix(r)
prefix := manager.GetProxyPrefix(r)
http.Redirect(w, r, prefix+"/", http.StatusFound)
}
return
Expand All @@ -80,7 +80,7 @@ func handleLoginPost() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
url := r.FormValue(returnURLParam)
if url == "" {
url = getProxyPrefix(r) + "/"
url = manager.GetProxyPrefix(r) + "/"
}

err := manager.GetInstance().SessionStore.Login(w, r)
Expand Down Expand Up @@ -114,7 +114,7 @@ func handleLogout() http.HandlerFunc {
}

// redirect to the login page if credentials are required
prefix := getProxyPrefix(r)
prefix := manager.GetProxyPrefix(r)
if config.GetInstance().HasCredentials() {
http.Redirect(w, r, prefix+loginEndpoint, http.StatusFound)
} else {
Expand Down
101 changes: 101 additions & 0 deletions internal/heresphere/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package heresphere

import (
"encoding/json"
"fmt"
"net/http"
"net/url"

"github.com/stashapp/stash/internal/manager"
"github.com/stashapp/stash/internal/manager/config"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/session"
)

/*
* This auxiliary function finds if a login is needed, and auth is correct.
*/
func basicLogin(username string, password string) bool {
// If needs creds, try login
if config.GetInstance().HasCredentials() {
err := manager.GetInstance().SessionStore.LoginPlain(username, password)
return err != nil
}
return false
}

/*
* This auxiliary function finds if the request has a valid auth token.
*/
func HeresphereHasValidToken(r *http.Request) bool {
// Check header auth
apiKey := r.Header.Get(HeresphereAuthHeader)

// Check url query auth
if len(apiKey) == 0 {
apiKey = r.URL.Query().Get(session.ApiKeyParameter)
}

return len(apiKey) > 0 && apiKey == config.GetInstance().GetAPIKey()
}

/*
* This auxiliary function adds an auth token to a url
*/
func addApiKey(urlS string) string {
// Parse URL
u, err := url.Parse(urlS)
if err != nil {
// shouldn't happen
panic(err)
}

// Add apikey if applicable
if config.GetInstance().GetAPIKey() != "" {
v := u.Query()
if !v.Has("apikey") {
v.Set("apikey", config.GetInstance().GetAPIKey())
}
u.RawQuery = v.Encode()
}

return u.String()
}

/*
* This auxiliary writes a library with a fake name upon auth failure
*/
func writeNotAuthorized(w http.ResponseWriter, r *http.Request, msg string) {
// Banner
banner := HeresphereBanner{
Image: fmt.Sprintf("%s%s",
manager.GetBaseURL(r),
"/apple-touch-icon.png",
),
Link: fmt.Sprintf("%s%s",
manager.GetBaseURL(r),
"/",
),
}
// Default video
library := HeresphereIndexEntry{
Name: msg,
List: []string{},
}
// Index
idx := HeresphereIndex{
Access: HeresphereBadLogin,
Banner: banner,
Library: []HeresphereIndexEntry{library},
}

// Create a JSON encoder for the response writer
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)
if err := enc.Encode(idx); err != nil {
logger.Errorf("Heresphere writeNotAuthorized error: %s\n", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
Loading
Loading