Skip to content

Commit

Permalink
edited Authorization token, rmv dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
kingmariano committed Jul 10, 2024
1 parent c052d34 commit 4bd4aab
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 68 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ RUN apt-get update && apt-get install -y \

# Verify installations
RUN ffmpeg -version
RUN tesseract --version
RUN tesseract --version

# Log the Tesseract installation path
RUN which tesseract

# Ensure the Go binary is executable
RUN chmod +x /app/omnicron
Expand Down
9 changes: 7 additions & 2 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@ package auth
import (
"errors"
"net/http"
"strings"
)

var ErrNoAuthHeaderIncluded = errors.New("no auth header included in request")

func GetHeaderToken(headers http.Header) (string, error) {
authHeader := headers.Get("Api-Key")
authHeader := headers.Get("Authorization")
if authHeader == "" {
return "", ErrNoAuthHeaderIncluded
}
splitAuth := strings.Split(authHeader, " ")
if len(splitAuth) < 2 || splitAuth[0] != "Bearer" {
return "", errors.New("malformed authorization header")
}

return authHeader, nil
return splitAuth[1], nil
}
59 changes: 36 additions & 23 deletions internal/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,61 @@
package auth

import (
"errors"
"net/http"
"testing"
)

func TestGetHeaderToken_MultipleValues(t *testing.T) {
func TestGetHeaderToken(t *testing.T) {
tests := []struct {
name string
headers http.Header
want string
wantErr error
name string
headers http.Header
expected string
expectedErr error
}{
{
name: "Single value in Api-Key header",
headers: http.Header{"Api-Key": []string{"value1"}},
want: "value1",
name: "No Authorization header",
headers: http.Header{},
expected: "",
expectedErr: ErrNoAuthHeaderIncluded,
},
{
name: "No Api-Key header",
headers: http.Header{},
wantErr: ErrNoAuthHeaderIncluded,
name: "Malformed Authorization header without Bearer",
headers: http.Header{
"Authorization": {"Token abcdef12345"},
},
expected: "",
expectedErr: errors.New("malformed authorization header"),
},
{
name: "Empty Api-Key header",
headers: http.Header{"Api-Key": []string{""}},
wantErr: ErrNoAuthHeaderIncluded,
name: "Malformed Authorization header with only Bearer",
headers: http.Header{
"Authorization": {"Bearer"},
},
expected: "",
expectedErr: errors.New("malformed authorization header"),
},
{
name: "Empty Api-Key header with other headers",
headers: http.Header{"Api-Key": []string{""}, "Other-Header": []string{"value"}},
wantErr: ErrNoAuthHeaderIncluded,
name: "Valid Authorization header",
headers: http.Header{
"Authorization": {"Bearer abcdef12345"},
},
expected: "abcdef12345",
expectedErr: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetHeaderToken(tt.headers)
if err != tt.wantErr {
t.Errorf("GetHeaderToken() error = %v, wantErr %v", err, tt.wantErr)
return
token, err := GetHeaderToken(tt.headers)
if token != tt.expected {
t.Errorf("expected token %v, got %v", tt.expected, token)
}
if got != tt.want {
t.Errorf("GetHeaderToken() got = %v, want %v", got, tt.want)
if err != nil && err.Error() != tt.expectedErr.Error() {
t.Errorf("expected error %v, got %v", tt.expectedErr, err)
}
if err == nil && tt.expectedErr != nil {
t.Errorf("expected error %v, got nil", tt.expectedErr)
}
})
}
Expand Down
41 changes: 0 additions & 41 deletions python/Dockerfile

This file was deleted.

Binary file modified python/endpoints/__pycache__/router.cpython-312.pyc
Binary file not shown.
37 changes: 36 additions & 1 deletion python/endpoints/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""

import os
import subprocess
import aiofiles
import asyncio
from pathlib import Path # Standard library imports
Expand Down Expand Up @@ -272,4 +273,38 @@ async def image_to_text(file: UploadFile = File(...), _: str = Depends(check_api
finally:
# Delete the temporary file
if temp_file_path.exists():
temp_file_path.unlink()
temp_file_path.unlink()


@router.get("/check_tesseract_installation_path")
async def check_tesseract_installation_path(_: str = Depends(check_api_key)):
"""
Check the installation path of Tesseract and if the eng.traineddata file exists.
"""
try:
# Find the Tesseract installation path
tesseract_path = subprocess.check_output(['which', 'tesseract']).decode().strip()

# Check if the eng.traineddata file exists in the specified TESSDATA_PREFIX or common directories
traineddata_paths = [
Path(tessdata_prefix) / 'eng.traineddata',
Path('/usr/share/tesseract-ocr/4.00/tessdata') / 'eng.traineddata',
Path('/usr/share/tesseract-ocr/tessdata') / 'eng.traineddata',
Path('/usr/local/share/tessdata') / 'eng.traineddata',
Path('/usr/share/tessdata') / 'eng.traineddata',
Path('/usr/share/tesseract/tessdata') / 'eng.traineddata',
Path('/var/lib/tesseract-ocr/tessdata') / 'eng.traineddata'
]

for path in traineddata_paths:
if path.exists():
eng_traineddata_path = path
break
else:
raise FileNotFoundError("eng.traineddata not found in any known directories")

return JSONResponse(content={"tesseract_path": tesseract_path, "eng_traineddata_path": str(eng_traineddata_path)})
except subprocess.CalledProcessError:
raise HTTPException(status_code=500, detail="Tesseract is not installed or not found in PATH")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))

0 comments on commit 4bd4aab

Please sign in to comment.