mirror of
https://github.com/RoboSats/robosats.git
synced 2025-07-19 09:13:28 +00:00
nostr pubkey for Robot
This commit is contained in:
@ -693,6 +693,7 @@ class RobotView(APIView):
|
|||||||
context["encrypted_private_key"] = user.robot.encrypted_private_key
|
context["encrypted_private_key"] = user.robot.encrypted_private_key
|
||||||
context["earned_rewards"] = user.robot.earned_rewards
|
context["earned_rewards"] = user.robot.earned_rewards
|
||||||
context["wants_stealth"] = user.robot.wants_stealth
|
context["wants_stealth"] = user.robot.wants_stealth
|
||||||
|
context["nostr_pubkey"] = user.robot.nostr_pubkey
|
||||||
context["last_login"] = user.last_login
|
context["last_login"] = user.last_login
|
||||||
|
|
||||||
# Adds/generate telegram token and whether it is enabled
|
# Adds/generate telegram token and whether it is enabled
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import { apiClient } from '../services/api';
|
import { apiClient, type Auth } from '../services/api';
|
||||||
import type Federation from './Federation.model';
|
import type Federation from './Federation.model';
|
||||||
import { type AuthHeaders } from './Slot.model';
|
|
||||||
|
|
||||||
class Robot {
|
class Robot {
|
||||||
constructor(attributes?: Record<any, any>) {
|
constructor(attributes?: Record<any, any>) {
|
||||||
@ -31,13 +30,15 @@ class Robot {
|
|||||||
Object.assign(this, attributes);
|
Object.assign(this, attributes);
|
||||||
};
|
};
|
||||||
|
|
||||||
getAuthHeaders = (): AuthHeaders | null => {
|
getAuthHeaders = (): Auth | null => {
|
||||||
const tokenSHA256 = this.tokenSHA256 ?? '';
|
const tokenSHA256 = this.tokenSHA256 ?? '';
|
||||||
const encPrivKey = this.encPrivKey ?? '';
|
const encPrivKey = this.encPrivKey ?? '';
|
||||||
const pubKey = this.pubKey ?? '';
|
const pubKey = this.pubKey ?? '';
|
||||||
|
const nostrPubkey = this.nostrPubKey ?? '';
|
||||||
|
|
||||||
return {
|
return {
|
||||||
tokenSHA256,
|
tokenSHA256,
|
||||||
|
nostrPubkey,
|
||||||
keys: {
|
keys: {
|
||||||
pubKey: pubKey.split('\n').join('\\'),
|
pubKey: pubKey.split('\n').join('\\'),
|
||||||
encPrivKey: encPrivKey.split('\n').join('\\'),
|
encPrivKey: encPrivKey.split('\n').join('\\'),
|
||||||
|
@ -5,14 +5,6 @@ import { roboidentitiesClient } from '../services/Roboidentities/Web';
|
|||||||
import { hexToBase91, validateTokenEntropy } from '../utils';
|
import { hexToBase91, validateTokenEntropy } from '../utils';
|
||||||
import { getPublicKey } from 'nostr-tools';
|
import { getPublicKey } from 'nostr-tools';
|
||||||
|
|
||||||
export interface AuthHeaders {
|
|
||||||
tokenSHA256: string;
|
|
||||||
keys: {
|
|
||||||
pubKey: string;
|
|
||||||
encPrivKey: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
class Slot {
|
class Slot {
|
||||||
constructor(
|
constructor(
|
||||||
token: string,
|
token: string,
|
||||||
|
@ -19,11 +19,11 @@ class ApiNativeClient implements ApiClient {
|
|||||||
Authorization: `Token ${auth.tokenSHA256}`,
|
Authorization: `Token ${auth.tokenSHA256}`,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
} else if (auth?.keys != null) {
|
} else if (auth?.keys != null && auth.nostrPubkey != null) {
|
||||||
headers = {
|
headers = {
|
||||||
...headers,
|
...headers,
|
||||||
...{
|
...{
|
||||||
Authorization: `Token ${auth.tokenSHA256} | Public ${auth.keys.pubKey} | Private ${auth.keys.encPrivKey}`,
|
Authorization: `Token ${auth.tokenSHA256} | Public ${auth.keys.pubKey} | Private ${auth.keys.encPrivKey} | Nostr ${auth.nostrPubkey}`,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -15,11 +15,11 @@ class ApiWebClient implements ApiClient {
|
|||||||
Authorization: `Token ${auth.tokenSHA256}`,
|
Authorization: `Token ${auth.tokenSHA256}`,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
} else if (auth?.keys != null) {
|
} else if (auth?.keys != null && auth.nostrPubkey != null) {
|
||||||
headers = {
|
headers = {
|
||||||
...headers,
|
...headers,
|
||||||
...{
|
...{
|
||||||
Authorization: `Token ${auth.tokenSHA256} | Public ${auth.keys.pubKey} | Private ${auth.keys.encPrivKey}`,
|
Authorization: `Token ${auth.tokenSHA256} | Public ${auth.keys.pubKey} | Private ${auth.keys.encPrivKey} | Nostr ${auth.nostrPubkey}`,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ import ApiNativeClient from './ApiNativeClient';
|
|||||||
|
|
||||||
export interface Auth {
|
export interface Auth {
|
||||||
tokenSHA256: string;
|
tokenSHA256: string;
|
||||||
|
nostrPubkey?: string;
|
||||||
keys?: { pubKey: string; encPrivKey: string };
|
keys?: { pubKey: string; encPrivKey: string };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,9 +42,15 @@ class SplitAuthorizationHeaderMiddleware(MiddlewareMixin):
|
|||||||
split_auth = auth_header.split(" | ")
|
split_auth = auth_header.split(" | ")
|
||||||
|
|
||||||
if len(split_auth) == 3:
|
if len(split_auth) == 3:
|
||||||
|
# Deprecated in favor of len 4
|
||||||
request.META["HTTP_AUTHORIZATION"] = split_auth[0]
|
request.META["HTTP_AUTHORIZATION"] = split_auth[0]
|
||||||
request.META["PUBLIC_KEY"] = split_auth[1]
|
request.META["PUBLIC_KEY"] = split_auth[1]
|
||||||
request.META["ENCRYPTED_PRIVATE_KEY"] = split_auth[2]
|
request.META["ENCRYPTED_PRIVATE_KEY"] = split_auth[2]
|
||||||
|
elif len(split_auth) == 4:
|
||||||
|
request.META["HTTP_AUTHORIZATION"] = split_auth[0]
|
||||||
|
request.META["PUBLIC_KEY"] = split_auth[1]
|
||||||
|
request.META["ENCRYPTED_PRIVATE_KEY"] = split_auth[2]
|
||||||
|
request.META["NOSTR_PUBKEY"] = split_auth[3]
|
||||||
|
|
||||||
|
|
||||||
class RobotTokenSHA256AuthenticationMiddleWare:
|
class RobotTokenSHA256AuthenticationMiddleWare:
|
||||||
@ -108,11 +114,13 @@ class RobotTokenSHA256AuthenticationMiddleWare:
|
|||||||
# Authorization header or in the Cookies.
|
# Authorization header or in the Cookies.
|
||||||
public_key = ""
|
public_key = ""
|
||||||
encrypted_private_key = ""
|
encrypted_private_key = ""
|
||||||
|
nostr_pubkey = ""
|
||||||
|
|
||||||
public_key = request.META.get("PUBLIC_KEY", "").replace("Public ", "")
|
public_key = request.META.get("PUBLIC_KEY", "").replace("Public ", "")
|
||||||
encrypted_private_key = request.META.get(
|
encrypted_private_key = request.META.get(
|
||||||
"ENCRYPTED_PRIVATE_KEY", ""
|
"ENCRYPTED_PRIVATE_KEY", ""
|
||||||
).replace("Private ", "")
|
).replace("Private ", "")
|
||||||
|
nostr_pubkey = request.META.get("NOSTR_PUBKEY", "").replace("Nostr ", "")
|
||||||
|
|
||||||
# Some legacy (pre-federation) clients will still send keys as cookies
|
# Some legacy (pre-federation) clients will still send keys as cookies
|
||||||
if public_key == "" or encrypted_private_key == "":
|
if public_key == "" or encrypted_private_key == "":
|
||||||
@ -158,6 +166,10 @@ class RobotTokenSHA256AuthenticationMiddleWare:
|
|||||||
if not user.robot.encrypted_private_key:
|
if not user.robot.encrypted_private_key:
|
||||||
user.robot.encrypted_private_key = encrypted_private_key
|
user.robot.encrypted_private_key = encrypted_private_key
|
||||||
|
|
||||||
|
# Add nostr key to the new user
|
||||||
|
if not user.robot.nostr_pubkey:
|
||||||
|
user.robot.nostr_pubkey = nostr_pubkey
|
||||||
|
|
||||||
update_last_login(None, user)
|
update_last_login(None, user)
|
||||||
user.save()
|
user.save()
|
||||||
|
|
||||||
|
@ -70,11 +70,12 @@ class Trade:
|
|||||||
b91_token = read_file(f"tests/robots/{robot_index}/b91_token")
|
b91_token = read_file(f"tests/robots/{robot_index}/b91_token")
|
||||||
pub_key = read_file(f"tests/robots/{robot_index}/pub_key")
|
pub_key = read_file(f"tests/robots/{robot_index}/pub_key")
|
||||||
enc_priv_key = read_file(f"tests/robots/{robot_index}/enc_priv_key")
|
enc_priv_key = read_file(f"tests/robots/{robot_index}/enc_priv_key")
|
||||||
|
nostr_pubkey = read_file(f"tests/robots/{robot_index}/nostr_pubkey")
|
||||||
|
|
||||||
# First time a robot authenticated, it is registered by the backend, so pub_key and enc_priv_key is needed
|
# First time a robot authenticated, it is registered by the backend, so pub_key and enc_priv_key is needed
|
||||||
if first_encounter:
|
if first_encounter:
|
||||||
headers = {
|
headers = {
|
||||||
"HTTP_AUTHORIZATION": f"Token {b91_token} | Public {pub_key} | Private {enc_priv_key}"
|
"HTTP_AUTHORIZATION": f"Token {b91_token} | Public {pub_key} | Private {enc_priv_key} | Nostr {nostr_pubkey}"
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
headers = {"HTTP_AUTHORIZATION": f"Token {b91_token}"}
|
headers = {"HTTP_AUTHORIZATION": f"Token {b91_token}"}
|
||||||
|
Reference in New Issue
Block a user