Signal-iOS/SignalMessaging/groups/StorageService+GroupsV2.swift
Evan Hahn 370ff654e7
Change license to AGPL
Change license to AGPL

This commit:

- Updates the `LICENSE` file

- Start every file with something like:

      // Copyright YEAR_FIRST_PUBLISHED Signal Messenger, LLC
      // SPDX-License-Identifier: AGPL-3.0-only

---

First, I removed existing license headers with this Ruby 3.1.2 script:

    require 'set'

    EXTENSIONS_TO_CHECK = Set['.h', '.hpp', '.cpp', '.m', '.mm', '.pch', '.swift']

    same = 0
    different = 0

    all_files = `git ls-files`.lines.map { |line| line.strip }
    all_files.each do |relative_path|
      if relative_path == 'Pods'
        next
      end

      unless EXTENSIONS_TO_CHECK.include? File.extname(relative_path)
        next
      end

      path = File.expand_path(relative_path)

      contents = File.read(path)
      new_contents = contents.sub(/\/\/\n\/\/  Copyright .*\n\/\/\n\n/, '')

      if contents == new_contents
        same += 1
      else
        different += 1
      end

      File.write(path, new_contents)
    end

    puts "updated #{different} file(s), left #{same} untouched"

I'm sure this script could be improved, but it worked well enough.

Then, I created `Scripts/lint/lint-license-headers` and ran it to auto-
fix a lot of files. This changed the mode of some files, but I think
that's actually desirable. For example,
`SignalServiceKit/src/Util/AppContext.m` previously had a mode of
`0755/-rwxr-xr-x`, and it's now `0644/-rw-r--r--`.

Then I fixed some stragglers and updated the precommit script.

See [a similar change in the Desktop app][0].

[0]: 8bfaf598af
2022-10-13 08:25:37 -05:00

172 lines
7.2 KiB
Swift

//
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
import SignalServiceKit
import LibSignalClient
public struct GroupsV2Request {
let urlString: String
let method: HTTPMethod
let bodyData: Data?
let headers = OWSHttpHeaders()
func addHeader(_ header: String, value: String) {
headers.addHeader(header, value: value, overwriteOnConflict: true)
}
}
// MARK: -
public extension StorageService {
static func buildNewGroupRequest(groupProto: GroupsProtoGroup,
groupV2Params: GroupV2Params,
authCredential: AuthCredentialWithPni) throws -> GroupsV2Request {
let protoData = try groupProto.serializedData()
return try buildGroupV2Request(protoData: protoData,
urlString: "/v1/groups/",
method: .put,
groupV2Params: groupV2Params,
authCredential: authCredential)
}
static func buildUpdateGroupRequest(groupChangeProto: GroupsProtoGroupChangeActions,
groupV2Params: GroupV2Params,
authCredential: AuthCredentialWithPni,
groupInviteLinkPassword: Data?) throws -> GroupsV2Request {
var urlString = "/v1/groups/"
if let groupInviteLinkPassword = groupInviteLinkPassword {
urlString += "?inviteLinkPassword=\(groupInviteLinkPassword.asBase64Url)"
}
let protoData = try groupChangeProto.serializedData()
return try buildGroupV2Request(protoData: protoData,
urlString: urlString,
method: .patch,
groupV2Params: groupV2Params,
authCredential: authCredential)
}
static func buildFetchCurrentGroupV2SnapshotRequest(
groupV2Params: GroupV2Params,
authCredential: AuthCredentialWithPni
) throws -> GroupsV2Request {
return try buildGroupV2Request(
protoData: nil,
urlString: "/v1/groups/",
method: .get,
groupV2Params: groupV2Params,
authCredential: authCredential
)
}
static func buildFetchGroupChangeActionsRequest(
groupV2Params: GroupV2Params,
fromRevision: UInt32,
requireSnapshotForFirstChange: Bool,
authCredential: AuthCredentialWithPni
) throws -> GroupsV2Request {
let urlPath = "/v1/groups/logs/\(fromRevision)?includeFirstState=\(requireSnapshotForFirstChange)&maxSupportedChangeEpoch=\(GroupManager.changeProtoEpoch)"
return try buildGroupV2Request(
protoData: nil,
urlString: urlPath,
method: .get,
groupV2Params: groupV2Params,
authCredential: authCredential
)
}
static func buildGetJoinedAtRevisionRequest(
groupV2Params: GroupV2Params,
authCredential: AuthCredentialWithPni
) throws -> GroupsV2Request {
return try buildGroupV2Request(
protoData: nil,
urlString: "/v1/groups/joined_at_version/",
method: .get,
groupV2Params: groupV2Params,
authCredential: authCredential
)
}
static func buildGroupAvatarUploadFormRequest(groupV2Params: GroupV2Params,
authCredential: AuthCredentialWithPni) throws -> GroupsV2Request {
let urlPath = "/v1/groups/avatar/form"
return try buildGroupV2Request(protoData: nil,
urlString: urlPath,
method: .get,
groupV2Params: groupV2Params,
authCredential: authCredential)
}
// inviteLinkPassword is not necessary if we're already a member or have a pending request.
static func buildFetchGroupInviteLinkPreviewRequest(inviteLinkPassword: Data?,
groupV2Params: GroupV2Params,
authCredential: AuthCredentialWithPni) throws -> GroupsV2Request {
var urlPath = "/v1/groups/join/"
if let inviteLinkPassword = inviteLinkPassword {
urlPath += "\(inviteLinkPassword.asBase64Url)"
}
return try buildGroupV2Request(protoData: nil,
urlString: urlPath,
method: .get,
groupV2Params: groupV2Params,
authCredential: authCredential)
}
static func buildFetchGroupExternalCredentials(groupV2Params: GroupV2Params,
authCredential: AuthCredentialWithPni) throws -> GroupsV2Request {
return try buildGroupV2Request(protoData: nil,
urlString: "/v1/groups/token",
method: .get,
groupV2Params: groupV2Params,
authCredential: authCredential)
}
private static func buildGroupV2Request(protoData: Data?,
urlString: String,
method: HTTPMethod,
groupV2Params: GroupV2Params,
authCredential: AuthCredentialWithPni) throws -> GroupsV2Request {
let request = GroupsV2Request(urlString: urlString, method: method, bodyData: protoData)
// The censorship circumvention reflectors require a Content-Type
// even if the body is empty.
request.addHeader("Content-Type", value: OWSMimeTypeProtobuf)
try self.addAuthorizationHeader(to: request,
groupV2Params: groupV2Params,
authCredential: authCredential)
return request
}
// MARK: - Authorization Headers
private static func addAuthorizationHeader(to request: GroupsV2Request,
groupV2Params: GroupV2Params,
authCredential: AuthCredentialWithPni) throws {
let serverPublicParams = try GroupsV2Protos.serverPublicParams()
let clientZkAuthOperations = ClientZkAuthOperations(serverPublicParams: serverPublicParams)
let authCredentialPresentation = try clientZkAuthOperations.createAuthCredentialPresentation(groupSecretParams: groupV2Params.groupSecretParams, authCredential: authCredential)
let authCredentialPresentationData = authCredentialPresentation.serialize().asData
let username: String = groupV2Params.groupPublicParamsData.hexadecimalString
let password: String = authCredentialPresentationData.hexadecimalString
request.addHeader(OWSHttpHeaders.authHeaderKey,
value: try OWSHttpHeaders.authHeaderValue(username: username, password: password))
}
}