-
Notifications
You must be signed in to change notification settings - Fork 24
/
auth.py
167 lines (150 loc) · 4.66 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import uuid
import os
import configparser
import server
from aiohttp import web
API_KEY = None
set_api_key_html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BizyAir - Set API Key</title>
<style>
body {
background-color: #121212;
color: #ffffff;
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
text-align: center;
}
input[type="password"] {
padding: 10px;
margin: 10px;
border: 2px solid rgb(130, 88, 245);
border-radius: 5px;
background-color: #1e1e1e;
color: #ffffff;
width: 200px;
}
button {
padding: 10px 20px;
border: none;
border-radius: 5px;
background-color: rgb(130, 88, 245);
color: #ffffff;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
</style>
<script>
async function setApiKey() {
const apiKey = document.getElementById('apiKey').value;
const response = await fetch('/bizyair/set_api_key', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `api_key=${encodeURIComponent(apiKey)}`
});
if (response.ok) {
alert('API Key set successfully!');
if (window.opener) {
window.close();
}
} else {
alert('Failed to set API Key: ' + await response.text());
}
}
</script>
</head>
<body>
<div class="container">
<h1>Set API Key</h1>
<input type="password" id="apiKey" placeholder="Enter API Key">
<button onclick="setApiKey()">Set API Key</button>
<p>To get your key, visit <a href="https://cloud.siliconflow.cn" target="_blank">https://cloud.siliconflow.cn</a></p>
</div>
</body>
</html>
</div>
</body>
</html>
"""
@server.PromptServer.instance.routes.get("/bizyair/set-api-key")
async def set_api_key_page(request):
return web.Response(text=set_api_key_html, content_type="text/html")
@server.PromptServer.instance.routes.post("/bizyair/set_api_key")
async def set_api_key(request):
global API_KEY
data = await request.post()
api_key = data.get("api_key")
try:
if api_key:
response = web.Response(text="ok")
response.set_cookie("api_key", api_key, max_age=30 * 24 * 60 * 60)
API_KEY = api_key
return response
else:
return web.Response(
text="No token provided, please refer to cloud.siliconflow.cn to get the key",
status=400,
)
except Exception as e:
return web.Response(text=str(e), status=500)
@server.PromptServer.instance.routes.get("/bizyair/get_api_key")
async def get_api_key(request):
global API_KEY
api_key = ""
current_directory = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(current_directory, "api_key.ini")
if os.path.exists(file_path):
config = configparser.ConfigParser()
config.read(file_path)
api_key = config.get("auth", "api_key", fallback="").strip()
if api_key == "":
api_key = request.cookies.get("api_key")
try:
if api_key:
API_KEY = api_key
response = web.Response(text="ok")
return response
else:
return web.Response(
text="No api key found in cookie, please refer to cloud.siliconflow.cn to get the key",
status=404,
)
except Exception as e:
return web.Response(text="str(e)", status=500)
class SetAPIKey:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"API_KEY": ("STRING", {"default": "YOUR_API_KEY"}),
}
}
RETURN_TYPES = ()
FUNCTION = "set_api_key"
CATEGORY = "☁️BizyAir"
OUTPUT_NODE = True
def set_api_key(self, API_KEY="YOUR_API_KEY"):
return {"ui": {"api_key": (API_KEY,)}, "result": ()}
@classmethod
def IS_CHANGED(s, latent):
return uuid.uuid4().hex
NODE_CLASS_MAPPINGS = {
"BizyAirSetAPIKey": SetAPIKey,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"BizyAirSetAPIKey": "☁️Set SiliconCloud API Key(deprecated)",
}